C language to realize the linked list of the two-way list (vii) After the end of the node insertion point
In the last article, we give the function of removing the head node, this article will give the function of inserting the node after the end node. Pay attention to the processing of the forward pointer in the bidirectional linked list, and learn from the one-way link list.
/*============================================================================== * Operation: Add a new node after the end point * Operation: PHeadNod E is the head pointer of the linked list, listdata for the added node data element * Operation: The node of the data element is Listdata is added to the trailing node, the header node is returned successfully, otherwise null ====================================
==========================================*/mylistnode* Inserttailnode (mylistnode* pHeadNode, MyListData ListData)
{mylistnode* plistnodetmp = Pheadnode;
mylistnode* pListNodeTmp1 = NULL;
Determine if a linked list is entered if (Pheadnode = = NULL) {fprintf (stderr, "There is no list.\n");
return NULL;
///Allocate memory for new node PLISTNODETMP1 = Mallocmeminitnode (PLISTNODETMP1);
if (pListNodeTmp1 = = NULL) {fprintf (stderr, "The new node ' s memory allocating defeat.\n");
return NULL;
} plistnodetmp1->snodedata = Listdata;
Move the pointer to the tail node while (plistnodetmp->pnextnodeaddr!= NULL) {plistnodetmp = plistnodetmp->pnextnodeaddr;
} plistnodetmp->pnextnodeaddr = PLISTNODETMP1; PLISTNODETMP1->PPREVNODEADDR = plistnodetmp;
return pheadnode; }
Highlights points:
(1) The function still calls the Mallocmeminitnode () function to allocate memory, but the data is passed in by the function;
(2) The function still makes error judgment and error handling;
(3) Remember to return the head node.