LinkedList.h (1494B)
1 #ifndef LINKEDLIST_H 2 #define LINKEDLIST_H 3 4 // A linked list containing any type of pointer. 5 // The linked list does _not_ own its elements. 6 7 typedef struct LinkedList LinkedList; 8 typedef struct LinkedListNode LinkedListNode; 9 10 struct LinkedList { 11 LinkedListNode *head; 12 LinkedListNode *tail; 13 int size; 14 }; 15 16 struct LinkedListNode { 17 LinkedListNode *next; 18 LinkedListNode *prev; 19 void *data; 20 }; 21 22 // Allocate and initialize an empty linked list. 23 // Returns: a pointer to the new linked list, or NULL on error. 24 // Post: the caller owns the linked list. 25 LinkedList *LinkedList_new(); 26 27 // Deallocate the given linked list, including all nodes. 28 void LinkedList_delete(LinkedList *ll); 29 30 // Append a the given element to the list. 31 // The linked list does _not_ take ownership over the element 32 // (only the linked list node). 33 // Returns: a pointer to the node with the new element, or NULL on error. 34 LinkedListNode *LinkedList_append(LinkedList *ll, void *elem); 35 36 // Remove and return the first element from the given list. 37 // Pre: ll->size != 0 38 void *LinkedList_popFront(LinkedList *ll); 39 40 // Find the linked list node containing the given element. 41 // Returns: a pointer to the found node, or NULL if the element was not found. 42 LinkedListNode *LinkedList_find(LinkedList *ll, void *elem); 43 44 // Remove the given node from the given linked list (and deallocate it). 45 // Pre: node must belong to ll 46 // Returns: node->data 47 void *LinkedList_remove(LinkedList *ll, LinkedListNode *node); 48 49 #endif // LINKEDLIST_H