Creating and Using Linked Lists

Vermont Views uses the linked list facility described here to create any linked lists needed internally by the system. Linked lists are useful when creating an ordered list of items which take different amounts of memory or if you are unsure of the size that each item will take.

The linked list facility is a circular double-linked list. Each node in the linked list is a structure of type NODE, which contains a pointer to the previous node, a pointer to the next node, and a pointer to any desired type of data. To access the data from any node, use:

ndp->datap

To initialize a linked list by creating a header node, call nd_inithd():

NODEPTR nd_inithd()

The header node never contains data. The data pointer is a NULLP.

To install nodes in the generic linked list, call nd_ins():

NODEPTR nd_ins(prev_ndp, dsize)
 
NODEPTR prev_ndp;
/* Pointer to the previous node
*/
UINT dsize;
/* Size of data element to allocate
*/

To install the first node, specify the header node as the previous node pointer. nd_ins() allocates memory for the node and installs it in the linked list. All previous and next node pointers in this node and the previous and next nodes are set appropriately. Immediately after this call, you should initialize the memory allocated for the data.

If you specify 0 for dsize, then you can install a pointer to data for the node data pointer. For example:

UCHAR *stp = "MY STRING";

hd_ndp = nd_inithd();

ndp = nd_ins(hd_ndp, 0);

ndp->datap = stp;

More:

Deleting a Node from a Linked List

Accessing Pointers to Nodes in a Linked List


Home Contents Previous Next