If there is a head node and a tail node in a bidirectional linked list, it is very convenient for the head interpolation and the tail interpolation method. This inserts an element into the tail without having to traverse the list. It is recommended that you use this linked list to handle problems. Code uploaded to Https://github.com/chenyufeng1991/HeadInsertAndTailInsert_DoubleList_HeadList.
The core code is as follows:
Create a bidirectional non-cyclic chain table (header interpolation) void Headinsertcreatelist (node *phead,node *ptail) {node *pinsert for the lead node and tail node;
Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->prior = NULL;
Pinsert->next = NULL;
scanf ("%d",& (pinsert->element));
while (Pinsert->element > 0) {phead->next->prior = Pinsert;
Pinsert->next = phead->next;
Pinsert->prior = Phead;
Phead->next = Pinsert;
Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->prior = NULL;
Pinsert->next = NULL;
scanf ("%d",& (pinsert->element));
printf ("%s function completed, header interpolation to establish the leading node and the trailing nodes of the bidirectional non circular chain table creation succeeded \ n", __function__);
///Create the bidirectional non-cyclic link table (tailinsertcreatelist) Void (node *phead,node *ptail) {node *pinsert) of the lead node and the tail node;
Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->prior = NULL; Pinsert->next = NULL
scanf ("%d",& (pinsert->element));
while (Pinsert->element > 0) {ptail->prior->next = Pinsert;
Pinsert->prior = ptail->prior;
Pinsert->next = Ptail;
Ptail->prior = Pinsert;
Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->prior = NULL;
Pinsert->next = NULL;
scanf ("%d",& (pinsert->element));
printf ("%s function completed, trailing interpolation to establish the leading node and the end point of the bidirectional Non circular chain table creation succeeded \ n", __function__);
}
The test code is as follows:
int main (int argc, const char * argv[]) {
node *phead;//header node
*ptail;//tail node
initiallist (&phead, & Ptail);
Headinsertcreatelist (Phead, ptail);
Printlist (Phead, ptail);
Tailinsertcreatelist (Phead, ptail);
Printlist (Phead, ptail);
return 0;
}