Manage a linked list (for internal use only). More...
#include "FreeList.hpp"
Go to the source code of this file.
Classes | |
struct | ListNode |
Linked list node. Stores generic item and pointers to next and prev. More... | |
struct | LinkedList |
Linked list (no protection). More... | |
Macros | |
#define | EOUTOFMEM (-7 & 1 << 29) |
Error condition for "out of memory". | |
Typedefs | |
typedef void(* | free_function) (void *arg) |
Function for freeing list items. | |
typedef int(* | cmp_routine) (void *itemA, void *itemB) |
Function for comparing list items. Returns 1 if itemA==itemB. | |
Functions | |
int | ListInit (LinkedList *list, cmp_routine cmp_func, free_function free_func) |
Initializes LinkedList. Must be called first and only once for List. | |
ListNode * | ListAddHead (LinkedList *list, void *item) |
Adds a node to the head of the list. Node gets immediately after list head. | |
ListNode * | ListAddTail (LinkedList *list, void *item) |
Adds a node to the tail of the list. Node gets added immediately before list.tail. | |
ListNode * | ListAddAfter (LinkedList *list, void *item, ListNode *bnode) |
Adds a node after the specified node. Node gets added immediately after bnode. | |
ListNode * | ListAddBefore (LinkedList *list, void *item, ListNode *anode) |
Adds a node before the specified node. Node gets added immediately before anode. | |
void * | ListDelNode (LinkedList *list, ListNode *dnode, int freeItem) |
Removes a node from the list. The memory for the node is freed. | |
int | ListDestroy (LinkedList *list, int freeItem) |
Removes all memory associated with list nodes. Does not free LinkedList *list. | |
ListNode * | ListHead (LinkedList *list) |
Returns the head of the list. | |
ListNode * | ListTail (LinkedList *list) |
Returns the tail of the list. | |
ListNode * | ListNext (LinkedList *list, ListNode *node) |
Returns the next item in the list. | |
ListNode * | ListPrev (LinkedList *list, ListNode *node) |
Returns the previous item in the list. | |
ListNode * | ListFind (LinkedList *list, ListNode *start, void *item) |
Finds the specified item in the list. | |
long | ListSize (LinkedList *list) |
Returns the size of the list. | |
Manage a linked list (for internal use only).
Because this is for internal use, parameters are NOT checked for validity. The caller must ensure valid parameters.
Definition in file LinkedList.hpp.
struct ListNode |
Linked list node. Stores generic item and pointers to next and prev.
Definition at line 58 of file LinkedList.hpp.
Class Members | ||
---|---|---|
struct ListNode * | prev | |
struct ListNode * | next | |
void * | item |
struct LinkedList |
Linked list (no protection).
The first item of the list is stored at node: head->next
The last item of the list is stored at node: tail->prev
If head->next=tail, then list is empty.
To iterate through the list:
LinkedList g; ListNode *temp = NULL; for (temp = ListHead(g);temp!=NULL;temp = ListNext(g,temp)) { }
Definition at line 79 of file LinkedList.hpp.
Class Members | ||
---|---|---|
ListNode | head | head, first item is stored at: head->next |
ListNode | tail |
tail, last item is stored at: tail->prev |
long | size | size of list |
FreeList | freeNodeList | free list to use |
free_function | free_func | free function to use |
cmp_routine | cmp_func | compare function to use |
#define EOUTOFMEM (-7 & 1 << 29) |
Error condition for "out of memory".
Definition at line 47 of file LinkedList.hpp.
typedef void(* free_function) (void *arg) |
Function for freeing list items.
Definition at line 50 of file LinkedList.hpp.
typedef int(* cmp_routine) (void *itemA, void *itemB) |
Function for comparing list items. Returns 1 if itemA==itemB.
Definition at line 53 of file LinkedList.hpp.
int ListInit | ( | LinkedList * | list, |
cmp_routine | cmp_func, | ||
free_function | free_func | ||
) |
Initializes LinkedList. Must be called first and only once for List.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | cmp_func | Function used to compare items. (May be NULL). |
[in] | free_func | Function used to free items. (May be NULL). |
Definition at line 95 of file LinkedList.cpp.
ListNode * ListAddHead | ( | LinkedList * | list, |
void * | item | ||
) |
Adds a node to the head of the list. Node gets immediately after list head.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | item | Item to be added. |
Definition at line 119 of file LinkedList.cpp.
ListNode * ListAddTail | ( | LinkedList * | list, |
void * | item | ||
) |
Adds a node to the tail of the list. Node gets added immediately before list.tail.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | item | Item to be added. |
Definition at line 128 of file LinkedList.cpp.
ListNode * ListAddAfter | ( | LinkedList * | list, |
void * | item, | ||
ListNode * | bnode | ||
) |
Adds a node after the specified node. Node gets added immediately after bnode.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | item | Item to be added. |
[in] | bnode | Node to add after. |
Definition at line 137 of file LinkedList.cpp.
ListNode * ListAddBefore | ( | LinkedList * | list, |
void * | item, | ||
ListNode * | anode | ||
) |
Adds a node before the specified node. Node gets added immediately before anode.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | item | Item to be added. |
[in] | anode | Node to add in front of. |
Definition at line 160 of file LinkedList.cpp.
void * ListDelNode | ( | LinkedList * | list, |
ListNode * | dnode, | ||
int | freeItem | ||
) |
Removes a node from the list. The memory for the node is freed.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | dnode | Node to delete. |
[in] | freeItem | if !0 then item is freed using free function. If 0 (or free function is NULL) then item is not freed. |
Definition at line 183 of file LinkedList.cpp.
int ListDestroy | ( | LinkedList * | list, |
int | freeItem | ||
) |
Removes all memory associated with list nodes. Does not free LinkedList *list.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | freeItem | If !0 then item is freed using free function. If 0 (or free function is NULL) then item is not freed. |
Definition at line 205 of file LinkedList.cpp.
ListNode * ListHead | ( | LinkedList * | list | ) |
Returns the head of the list.
Precondition: The list has been initialized.
list | [0] Must be valid, non null, pointer to a linked list. |
Definition at line 223 of file LinkedList.cpp.
ListNode * ListTail | ( | LinkedList * | list | ) |
Returns the tail of the list.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
Definition at line 235 of file LinkedList.cpp.
ListNode * ListNext | ( | LinkedList * | list, |
ListNode * | node | ||
) |
Returns the next item in the list.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | node | Node from the list. |
Definition at line 247 of file LinkedList.cpp.
ListNode * ListPrev | ( | LinkedList * | list, |
ListNode * | node | ||
) |
Returns the previous item in the list.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | node | Node from the list. |
Definition at line 259 of file LinkedList.cpp.
ListNode * ListFind | ( | LinkedList * | list, |
ListNode * | start, | ||
void * | item | ||
) |
Finds the specified item in the list.
Uses the compare function specified in ListInit. If compare function is nullptr then compares items as pointers.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
[in] | start | The node to start from, nullptr if to start from beginning. |
[in] | item | The item to search for. |
Definition at line 272 of file LinkedList.cpp.
long ListSize | ( | LinkedList * | list | ) |
Returns the size of the list.
Precondition: The list has been initialized.
[in] | list | Must be valid, non null, pointer to a linked list. |
Definition at line 300 of file LinkedList.cpp.