Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // NEVER forget your imports!
- #include <stdlib.h>
- // TO BE MEMCHECK'D WITH VALGRIND
- typedef struct node
- {
- int elem;
- struct node* next;
- } node;
- node* add(int elem, node* list)
- {
- node* out = malloc(sizeof(struct node));
- out->elem = elem;
- out->next = list;
- }
- void print(node* list)
- {
- if(list == NULL) return;
- printf("* %d *\n", list->elem);
- print(list->next);
- }
- void delete(node* list)
- {
- if(list == NULL) return;
- delete(list->next);
- free(list);
- printf("I deleted a node!\n");
- }
- int main()
- {
- node* list = add(5, add(3, add(2, add(7, add(4, add(1, NULL))))));
- print(list); // 5, 3, 2, 7, 4, 1
- delete(list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement