Skip to content

Instantly share code, notes, and snippets.

@alecbz
Created May 6, 2013 18:23
Show Gist options
  • Save alecbz/5527017 to your computer and use it in GitHub Desktop.
Save alecbz/5527017 to your computer and use it in GitHub Desktop.
#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