Skip to content

Instantly share code, notes, and snippets.

@kevinvaldek
Created February 25, 2010 14:57
Show Gist options
  • Save kevinvaldek/314593 to your computer and use it in GitHub Desktop.
Save kevinvaldek/314593 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct list_item {
struct list_item *next;
int data;
};
typedef struct list_item ListItem;
ListItem *head;
ListItem *tail;
ListItem *allocate_list_item() {
return (ListItem*) malloc(sizeof(ListItem));
}
void init_linked_list() {
head = allocate_list_item();
tail = allocate_list_item();
}
void push(int data) {
ListItem *new_item = allocate_list_item();
new_item->data = data;
if(tail->next == NULL) {
tail->next = new_item;
head->next = new_item;
}
else {
ListItem *current_tail = tail->next;
tail->next = new_item;
current_tail->next = new_item;
}
}
int first() {
if(head->next == NULL) {
printf("No items yet missy.\n");
return -1;
}
else {
ListItem *to_be_next = head->next->next;
int data = head->next->data;
free(head->next);
if(to_be_next != NULL) {
head->next = to_be_next;
}
else {
head->next = NULL;
tail->next = NULL;
}
return data;
}
}
int main() {
init_linked_list();
push(33);
printf("Firsty: %d\n", first());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment