/* * File: list1.c * Creator: George Ferguson * * ``Generic'' linked list, non-reusable version. */ #include #include #include "Employee.h" typedef struct Node* Node; struct Node { void* data; struct Node* next; }; 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; } typedef struct LinkedList* LinkedList; 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; } } int main(int argc, char* argv[]) { Employee e1 = new_Employee("Isaac Newton", 123); Employee e2 = new_Employee("Albert Einstein", 456); LinkedList list = new_LinkedList(); LinkedList_prepend(list, e1); LinkedList_prepend(list, e2); Employee e = (Employee)LinkedList_first(list); // *** char* name = Employee_get_name(e); printf("%s\n", name); // Albert Einstein }