Created
May 6, 2013 18:23
-
-
Save alecbz/5527017 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node { | |
int data; | |
struct node* next; | |
} node; | |
node* new_node() { | |
node* n = malloc(sizeof(node)); | |
n->next = NULL; | |
return n; | |
} | |
node* push_front(node* list, int data) { | |
node* new_head = new_node(); | |
new_head->data = data; | |
new_head->next = list; | |
return new_head; | |
} | |
int main() { | |
node* list = new_node(); | |
list->data = 1337; | |
int i; | |
for (i = 0; i < 10; ++i) { | |
list = push_front(list, (i + 1) * (i + 1)); | |
} | |
node* curr = list; | |
while (curr != NULL) { | |
printf("%d\n", curr->data); | |
curr = curr->next; | |
} | |
curr = list; | |
while (curr != NULL) { | |
node* next = curr->next; | |
free(curr); | |
curr = next; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment