[ create a new paste ] login | about

Link: http://codepad.org/wh78pI3a    [ raw code | output | fork ]

C, pasted on Aug 24:
#include<stdio.h>
#include<stdlib.h>

struct data{
  char key;
  struct data *next;
};

void print_stack_list(struct data *top) {
  while (top != 0) {
    printf("key: %c, next: %p\n", top->key, (void *)top->next);
    top = top->next;
  }
}
void release(struct data *top) {
  struct data *p;
  while (top != 0) {
    p = top->next;
    free(top);
    top = p;
  }
}

int main() {
  struct data *top, *cur;
  char c;
  top = 0;
  c = 'a';

  while (c <= 'd') {
    if ((cur = malloc(sizeof(struct data))) == 0) {
      fprintf(stderr, "cannnot allocate enough memory, aborted.\n");
      return 0;
    }
    cur->key = c;
    cur->next = top;
    top = cur;
    c++;
 }
  print_stack_list(top);
  release(top);
  return 0;
}
/* end */


Output:
1
2
3
4
key: d, next: 0x804a0c8
key: c, next: 0x804a0a0
key: b, next: 0x804a078
key: a, next: (nil)


Create a new paste based on this one


Comments: