Advertisement
heavenriver

node.c

Jun 7th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <stdio.h> // NEVER forget your imports!
  2. #include <stdlib.h>
  3.  
  4. // TO BE MEMCHECK'D WITH VALGRIND
  5.  
  6. typedef struct node
  7.  {
  8.  int elem;
  9.  struct node* next;
  10.  } node;
  11.  
  12. node* add(int elem, node* list)
  13.  {
  14.  node* out = malloc(sizeof(struct node));
  15.  out->elem = elem;
  16.  out->next = list;
  17.  }
  18.  
  19. void print(node* list)
  20.  {
  21.  if(list == NULL) return;
  22.  printf("* %d *\n", list->elem);
  23.  print(list->next);
  24.  }
  25.  
  26. void delete(node* list)
  27.  {
  28.  if(list == NULL) return;
  29.  delete(list->next);
  30.  free(list);
  31.  printf("I deleted a node!\n");
  32.  }
  33.  
  34.  int main()
  35.  {
  36.  node* list = add(5, add(3, add(2, add(7, add(4, add(1, NULL))))));
  37.  print(list); // 5, 3, 2, 7, 4, 1
  38.  delete(list);
  39.  return 0;
  40.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement