/* operation: initialize a list */ /* preconditions: plist points to a list */ /* postconditions: the list is initialized to empty */ voidInitializeList(List * plist);
/* operation: determine if list is empty */ /* plist points to an initialized list */ /* postconditions: function returns True if list is empty */ /* and returns False otherwise */ boolListIsEmpty(const List *plist);
/* operation: determine if list is full */ /* plist points to an initialized list */ /* postconditions: function returns True if list is full */ /* and returns False otherwise */ boolListIsFull(const List *plist);
/* operation: determine number of items in list */ /* plist points to an initialized list */ /* postconditions: function returns number of items in list */ unsignedintListItemCount(const List *plist);
/* operation: add item to end of list */ /* preconditions: item is an item to be added to list */ /* plist points to an initialized list */ /* postconditions: if possible, function adds item to end */ /* of list and returns True; otherwise the */ /* function returns False */ boolAddItem(Item item, List * plist);
/* operation: apply a function to each item in list */ /* plist points to an initialized list */ /* pfun points to a function that takes an */ /* Item argument and has no return value */ /* postcondition: the function pointed to by pfun is */ /* executed once for each item in the list */ voidTraverse(const List *plist, void (* pfun)(Item item) );
/* operation: free allocated memory, if any */ /* plist points to an initialized list */ /* postconditions: any memory allocated for the list is freed */ /* and the list is set to empty */ voidEmptyTheList(List * plist);
/* local function prototype */ staticvoidCopyToNode(Item item, Node * pnode);
/* interface functions */ /* set the list to empty */ voidInitializeList(List * plist) { * plist = NULL; }
/* returns true if list is empty */ boolListIsEmpty(const List * plist) { if (*plist == NULL) returntrue; else returnfalse; }
/* returns true if list is full */ boolListIsFull(const List * plist) { Node * pt; bool full; pt = (Node *) malloc(sizeof(Node)); if (pt == NULL) full = true; else full = false; free(pt); return full; }
/* returns number of nodes */ unsignedintListItemCount(const List * plist) { unsignedint count = 0; Node * pnode = *plist; /* set to start of list */ while (pnode != NULL) { ++count; pnode = pnode->next; /* set to next node */ } return count; }
/* creates node to hold item and adds it to the end of */ /* the list pointed to by plist (slow implementation) */ boolAddItem(Item item, List * plist) { Node * pnew; Node * scan = *plist; pnew = (Node *) malloc(sizeof(Node)); if (pnew == NULL) returnfalse; /* quit function on failure */ CopyToNode(item, pnew); pnew->next = NULL; if (scan == NULL) /* empty list, so place */ *plist = pnew; /* pnew at head of list */ else { while (scan->next != NULL) scan = scan->next; /* find end of list */ scan->next = pnew; /* add pnew to end */ } returntrue; }
/* visit each node and execute function pointed to by pfun */ voidTraverse(const List * plist, void (* pfun)(Item item) ) { Node * pnode = *plist; /* set to start of list */ while (pnode != NULL) { (*pfun)(pnode->item); /* apply function to item */ pnode = pnode->next; /* advance to next item */ } }
/* free memory allocated by malloc() */ /* set list pointer to NULL */ voidEmptyTheList(List * plist) { Node * psave; while (*plist != NULL) { psave = (*plist)->next; /* save address of next node */ free(*plist); /* free current node */ *plist = psave; /* advance to next node */ } }
/* local function definition */ /* copies an item into a node */ staticvoidCopyToNode(Item item, Node * pnode) { pnode->item = item; /* structure copy */ }