For two-way linked lists, it is more convenient for individuals to recommend the way to handle the end points of the lead node. I in the "C language to achieve two-way non-circular chain list (not the lead node) of the insertion of the nodes" in detail to achieve in the case of no lead node insertion. This time we will be going to insert the element at any location using the end node of the head node. Code uploaded to Https://github.com/chenyufeng1991/InsertNodeDoubleLinkedList_HeadNode.
The core code is as follows:
Insert a node
//insertion position for 0,1,2,.....
int insertnodelist (Node *phead,node *ptail,int pos,int x) {
int i = 0;
Node *pmove;
Node *pinsert;
Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->prior = NULL;
Pinsert->next = NULL;
pinsert->element = x;
Pmove = Phead;
while (Pmove!= ptail) {
if (i = = pos) {
//Note the link order here
pmove->next->prior = Pinsert;
Pinsert->next = pmove->next;
Pmove->next = Pinsert;
Pinsert->prior = Pmove;
printf ("%s function executes, insert x=%d node in pos=%d position \ n", __function__,pos,x);
return 1;
}
i++;
Pmove = pmove->next;
}
printf ("%s function execution, insert element failed \ \ \ __function__");
return 0;
}