/* * File: list1.c * Creator: George Ferguson * * Linked list of employees. */ #include #include #include "Employee.h" typedef struct Node* Node; struct Node { Employee data; struct Node* next; }; Node new_Node(Employee employee) { Node this = (Node)malloc(sizeof(struct Node)); if (this == NULL) { return NULL; // Out of memory } this->data = employee; 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, Employee employee) { Node node = new_Node(employee); if (node == NULL) { // Out of memory! } node->next = this->head; this->head = node; } Employee 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 = LinkedList_first(list); char* name = Employee_get_name(e); printf("%s\n", name); // Albert Einstein }