NOTE:
This project is no longer being maintained: it was developed for my masters thesis, which was completed in early 1997. I still, however, welcome any questions or comments that people may have.

[Home] [ToC] [Up] [Prev] [Next]


iHTML Library Services

Doubly-linked Lists

The iHTML library's doubly-linked list services are a standard set of types and functions for managing arbtrirary doubly-linked list structures. They are used by putting the IHNode structure at the type of another structure that is to be used in a list; the list management routines may then be used directly on that new structure's IHNode field.


Types

IHList
Synopsis
A handle on a doubly-linked list.
Definition
typedef struct ih_list_rec {
    IHNode    *lh_Head;       /* The first node in the list */
    IHNode    *lh_Tail;       /* The last node in the list */
} IHList;
See Also
IHNode, IH_AllocList(), IH_InitList()

This is a handle on a full doubly-linked list of nodes. It can be created two ways: dynamically by calling IH_AllocList(), and statically by reserving memory for the list structure and then calling IH_InitList() on it.

IHNode
Synopsis
A single node in a linked list.
Definition
typedef struct ih_node_rec {
    IHNode    *ln_Succ;       /* Pointer to next (successor) */
    IHNode    *ln_Pred;       /* Pointer to previous (predecessor) */
} IHNode;
See Also
IHList, IH_AllocNode()

This type represents a single node in a longer doubly-linked list of nodes. It can be appended to the top of any structure that needs to be put into a list. Any objects that incorporate this node should be allocated by calling IH_AllocNode().


Functions

IH_AddHead
Synopsis
void* IH_AddHead(IHList* ls,IHNode* nd)
Arguments
(IHList*) ls
A handle on a list.
(IHNode*) nd
A node to add to the list.
Return
(void*) The new head of the list, i.e. nd.
See Also
IHList, IHNode

Inserts the given node into the front of the list.

IH_AddTail
Synopsis
void* IH_AddTail(IHList* ls,IHNode* nd)
Arguments
(IHList*) ls
A handle on a list.
(IHNode*) nd
A node to add to the list.
Return
(void*) The new tail of the list, i.e. nd.
See Also
IHList, IHNode

Appends the given node to the back of the list.

IH_AllocNode
Synopsis
void* IH_AllocNode(int size)
Arguments
(int) size
Size, in bytes, of node structure to allocate.
Return
(void*) A new instance of the node.
See Also
IHNode

This function is used to allocate a new structure that is being used as a node. The size is the total size of the structure, include the IHNode at the top. Memory for the new structure is allocated, cleared to zeros, and returned.

IH_AllocList
Synopsis
IHList* IH_AllocList(void)
Arguments
none.
Return
(IHList*) A new list.
See Also
IHList,

Allocates and initializes a new list.

IH_FreeList
Synopsis
void IH_FreeList(IHList* ls)
Arguments
(IHList*) ls
A list of nodes.
Return
nothing.
See Also
IHList,

This function deallocates an IHList, and all nodes attached to it. This can be called on a non-empty list if its nodes can be deallocated by calling free() on them; otherwise, all nodes should be removed from the list and then it may be deallocated.

IH_GetHead
Synopsis
void* IH_GetHead(IHList* ls)
Arguments
(IHList*) ls
A handle on a list.
Return
(void*) The first node in the list.
See Also
IHList, IHNode

Returns the first node in a list. Returns NULL if the list is empty.

IH_GetTail
Synopsis
void* IH_GetTail(IHList* ls)
Arguments
(IHList*) ls
A handle on a list.
Return
(void*) The first node in the list.
See Also
IHList, IHNode

Returns the last node in a list. Returns NULL if the list is empty.

IH_InitList
Synopsis
void IH_InitList(IHList* ls)
Arguments
(IHList*) ls
A list of nodes.
Return
nothing.
See Also
IHList,

Initializes the fields on of IHList. This function should be called on any IHList that has not been created with IH_AllocList(), prior to using it.

IH_InsNode
Synopsis
void* IH_InsNode(IHList* ls,IHNode* place, IHNode* nd)
Arguments
(IHList*) ls
A handle on a list.
(IHNode*) place
Position in the list.
(IHNode*) nd
A node to add to the list.
Return
(void*) The new node located after the position, i.e. nd.
See Also
IHList, IHNode

Inserts the given node into the list, after the location indicated by place. If place is NULL, the node is inserted at the head of the list.

IH_NextNode
Synopsis
void* IH_NextNode(IHList* ls, IHNode* nd)
Arguments
(IHList*) ls
A handle on a list.
(IHNode*) nd
A node in the list
Return
(void*) The node that appears after the given node in the list.
/* Traverse forward through a list. */

IHNode* nd = NULL;
while( (nd=IH_NextNode(ls,nd)) ) {
  /* Do something with nd. */
}
See Also
IHList, IHNode

Returns the node that occurs after the given node in a list. If the given node is NULL, the first node in the list is returned.

IH_PrevNode
Synopsis
void* IH_PrevNode(IHList* ls, IHNode* nd)
Arguments
(IHList*) ls
A handle on a list.
(IHNode*) nd
A node in the list
Return
(void*) The node that appears prior the given node in the list.
/* Traverse backward through a list. */

IHNode* nd = NULL;
while( (nd=IH_PrevNode(ls,nd)) ) {
  /* Do something with nd. */
}
See Also
IHList, IHNode

Returns the node that occurs before the given node in a list. If the given node is NULL, the last node in the list is returned.

IH_RemHead
Synopsis
void* IH_RemHead(IHList* ls)
Arguments
(IHList*) ls
A handle on a list.
Return
(void*) The old head node of the list.
See Also
IHList, IHNode

Removes the head node from the list, and returns it. Returns NULL if the list is empty.

IH_RemNode
Synopsis
void* IH_RemNode(IHList* ls, IHNode* nd)
Arguments
(IHList*) ls
A handle on a list.
(IHNode*) nd
A node in the list
Return
(void*) The node removed from the list, i.e. nd.
See Also
IHList, IHNode

Removes the given node from the list, and returns it. The node must be in the list when this function is called.

IH_RemTail
Synopsis
void* IH_RemTail(IHList* ls)
Arguments
(IHList*) ls
A handle on a list.
Return
(void*) The old tail node of the list.
Example
/* Remove and deallocate all nodes from a list. */

IHNode* nd;
while( (nd=IH_RemTail(ls)) ) {
  free(nd);
}
See Also
IHList, IHNode

Removes the tail node from the list, and returns it. Returns NULL if the list is empty.


[Home] [ToC] [Up] [Prev] [Next]

_________.oo_Q_Q_oo.____________________________________________
Dianne Kyra Hackborn <hackbod@angryredplanet.com>
Last modified: Tue Oct 8 04:11:59 PDT 1996

This web page and all material contained herein is Copyright (c) 1997 Dianne Hackborn, unless otherwise noted. All rights reserved.