aboutsummaryrefslogtreecommitdiff
path: root/src/LinkedList.h
blob: 57538e28b6a500f707e24e7521ba024f93b9b929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

// A linked list containing any type of pointer.
// The linked list does _not_ own its elements.

typedef struct LinkedList LinkedList;
typedef struct LinkedListNode LinkedListNode;

struct LinkedList {
    LinkedListNode *head;
    LinkedListNode *tail;
    int size;
};

struct LinkedListNode {
    LinkedListNode *next;
    LinkedListNode *prev;
    void *data;
};

// Allocate and initialize an empty linked list.
// Returns: a pointer to the new linked list, or NULL on error.
// Post: the caller owns the linked list.
LinkedList *LinkedList_new();

// Deallocate the given linked list, including all nodes
// (but _not_ the data they point to, the user owns that).
void LinkedList_delete(LinkedList *ll);

// Append a the given element to the list.
// The linked list does _not_ take ownership over the element
// (only the linked list node).
// Returns: a pointer to the node with the new element, or NULL on error.
LinkedListNode *LinkedList_append(LinkedList *ll, void *elem);

// Remove and return the first element from the given list.
// Pre: ll->size != 0
void *LinkedList_popFront(LinkedList *ll);

// Find the linked list node containing the given element.
// Returns: a pointer to the found node, or NULL if the element was not found.
LinkedListNode *LinkedList_find(LinkedList *ll, void *elem);

// Remove the given node from the given linked list (and deallocate it).
// Pre: node must belong to ll
// Returns: node->data
void *LinkedList_remove(LinkedList *ll, LinkedListNode *node);

#endif // LINKEDLIST_H