LinkedList.h File Reference
`#include "FreeList.h"`
Include dependency graph for LinkedList.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures

struct | LISTNODE
struct | LINKEDLIST

## Macros

#define | EOUTOFMEM (-7 & 1 << 29)
#define | FREELISTSIZE 100
#define | LIST_SUCCESS 1
#define | LIST_FAIL 0

## Typedefs

typedef void(* | free_function) (void *arg)
typedef int(* | cmp_routine) (void *itemA, void *itemB)
typedef struct LISTNODE | ListNode
typedef struct LINKEDLIST | LinkedList

## 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.

## Typedef Documentation

## cmp_routine

typedef int(* cmp_routine) (void *itemA, void *itemB)

Function for comparing list items. Returns 1 if itemA==itemB

## free_function

typedef void(* free_function) (void *arg)

Function for freeing list items.

## LinkedList

typedef struct LINKEDLIST LinkedList

Linked list (no protection).

Because this is for internal use, parameters are NOT checked for validity. 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)) {
     }

## ListNode

typedef struct LISTNODE ListNode

Linked list node. Stores generic item and pointers to next and prev.

## Function Documentation

## ListAddAfter()

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.

Returns
  The pointer to the ListNode on success, NULL on failure.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  item | Item to be added.
  bnode | Node to add after.

References LINKEDLIST::size.

## ListAddBefore()

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.

Returns
  The pointer to the ListNode on success, NULL on failure.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  item | Item to be added.
  anode | Node to add in front of.

References LINKEDLIST::size.

## ListAddHead()

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.

Returns
  The pointer to the ListNode on success, NULL on failure.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  item | Item to be added.

References LINKEDLIST::head.

## ListAddTail()

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.

Returns
  The pointer to the ListNode on success, NULL on failure.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  item | Item to be added.

References LINKEDLIST::tail.

## ListDelNode()

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.

Returns
  The pointer to the item stored in the node or NULL if the item is freed.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  dnode | Node to delete.
  freeItem | if !0 then item is freed using free function. If 0 (or free function is NULL) then item is not freed.

References LINKEDLIST::free_func, LINKEDLIST::head, LINKEDLIST::size, and LINKEDLIST::tail.

## ListDestroy()

int ListDestroy | ( | LinkedList * | list,
|  | int | freeItem )

Removes all memory associated with list nodes. Does not free LinkedList *list.

Precondition: The list has been initialized.

Returns
  0 on success, EINVAL on failure.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  freeItem | if !0 then item is freed using free function. If 0 (or free function is NULL) then item is not freed.

References LINKEDLIST::freeNodeList, LINKEDLIST::head, LINKEDLIST::size, and LINKEDLIST::tail.

## ListFind()

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 NULL then compares items as pointers.

Precondition: The list has been initialized.

Returns
  The node containing the item. NULL if no node contains the item.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  start | The node to start from, NULL if to start from beginning.
  item | The item to search for.

References LINKEDLIST::cmp_func, LINKEDLIST::head, and LINKEDLIST::tail.

## ListHead()

ListNode * ListHead | ( | LinkedList * | list | ) |

Returns the head of the list.

Precondition: The list has been initialized.

Returns
  The head of the list. NULL if list is empty.

Parameters

  list | Must be valid, non null, pointer to a linked list.

References LINKEDLIST::head, and LINKEDLIST::size.

## ListInit()

int ListInit | ( | LinkedList * | list,
|  | cmp_routine | cmp_func,
|  | free_function | free_func )

Initializes LinkedList. Must be called first and only once for List.

Returns
  - `0` on success.
  - `EOUTOFMEM` on failure.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  cmp_func | Function used to compare items. (May be NULL).
  free_func | Function used to free items. (May be NULL).

References LINKEDLIST::cmp_func, LINKEDLIST::free_func, LINKEDLIST::freeNodeList, LINKEDLIST::head, LINKEDLIST::size, and LINKEDLIST::tail.

## ListNext()

ListNode * ListNext | ( | LinkedList * | list,
|  | ListNode * | node )

Returns the next item in the list.

Precondition: The list has been initialized.

Returns
  The next item in the list. NULL if there are no more items in list.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  node | Node from the list.

References LINKEDLIST::tail.

## ListPrev()

ListNode * ListPrev | ( | LinkedList * | list,
|  | ListNode * | node )

Returns the previous item in the list.

Precondition: The list has been initialized.

Returns
  The previous item in the list. NULL if there are no more items in list.

Parameters

  list | Must be valid, non null, pointer to a linked list.
  node | Node from the list.

References LINKEDLIST::head.

## ListSize()

long ListSize | ( | LinkedList * | list | ) |

Returns the size of the list.

Precondition: The list has been initialized.

Returns
  The number of items in the list.

Parameters

  list | Must be valid, non null, pointer to a linked list.

References LINKEDLIST::size.

## ListTail()

ListNode * ListTail | ( | LinkedList * | list | ) |

Returns the tail of the list.

Precondition: The list has been initialized.

Returns
  The tail of the list. NULL if list is empty.

Parameters

  list | Must be valid, non null, pointer to a linked list.

References LINKEDLIST::size, and LINKEDLIST::tail.