/* * File: LinkedList.c * Creator: George Ferguson * * ``Generic'' linked list in C. */ #include #include #include "LinkedList.h" struct Node { void* data; struct Node* next; }; typedef struct Node* Node; Node new_Node(void* data) { Node this = (Node)malloc(sizeof(struct Node)); if (this == NULL) { return NULL; // Out of memory } this->data = data; this->next = NULL; return this; } struct LinkedList { Node head; }; LinkedList new_LinkedList() { LinkedList this = (LinkedList)malloc(sizeof(struct LinkedList)); if (this == NULL) { return NULL; // Out of memory } this->head = NULL; return this; } void LinkedList_prepend(LinkedList this, void *data) { Node node = new_Node(data); if (node == NULL) { // Out of memory! } node->next = this->head; this->head = node; } void* LinkedList_first(LinkedList this) { if (this->head == NULL) { return NULL; } else { return this->head->data; } }