Commonly used lists/stacks are mister into a head pointer head then use the head pointer to request a head node space, and then the head node data is generally not stored
{The head becomes a node called head when memory is allocated with the malloc function.} Instead of being a separate pointer.}
{Note that only the space requested by malloc will allow the pointer to loop to a different memory space for the next time, that is, the memory address of each request is not the same, unless the memory space is free, it cannot be used again}
When the list/stack is empty, the head node head->next=null; otherwise point to the normal node that will store the data, (so that the empty type of the linked list/stack to determine the printing operation is more convenient)
The introduction of the lead node (which does not hold the normal data) is intended to be more uniform in the case of linked list deletions, reversals, and builds, without having to deal with the first element alone. 】
3. Most programs use the lead node to store the length of the linked list.
4, how to distinguish, very simple, look at the definition of the program, and the program in the establishment of the linked list when there is no special treatment for the first node. (for special handling, the head node stores the data/i.e. no head node, as long as the head pointer)
More detailed description of the blog, 80552680
#include <stdio.h>#include<stdlib.h>typedefintElementtype;typedefstructNode *Ptrtonode;structNode {ElementType Data; Ptrtonode Next;}; typedef ptrtonode List;//list is a struct Node pointerlist Merge (list L1, List L2); List Read (); /*details are not in this table*/voidPrint (List L);/*details are not table; empty list will output null*/intMain () {List l1,l2,l; L1=Read (); L2=Read ();//Print (L1);//Print (L2);L=Merge (L1, L2); printf ("\ n------\ n"); Print (L); Print (L1); Print (L2);} List Read () {list r,l=(structnode*)malloc(sizeof(structNode)); L->next=null;//Create a Head node, the general list is the leader node, the data of the head node is not stored, of course, you can also create a linked list without a head node ;//because a linked list with a head node can make the print operation more concise, the list has a head node that does not hold the data; "Linked list"R=L; intN; scanf ("%d",&N); while(n--) {///tail interpolation methodNode *p= (structnode*)malloc(sizeof(structNode)); scanf ("%d",&p->Data); P->next=NULL; R->next=p; R=p; } returnL;}voidPrint (List L) {if(l->Next) {List R=L; while(r->Next) {R=r->Next; printf ("%d",r->Data); } puts (""); } Else{printf ("null\n"); }}list Merge (List l1,list L2) {list p1,p2,p3,head; Head=(structnode*)malloc(sizeof(structNode)); P3=Head; P3->next=NULL; P1=l1->next,p2=l2->Next; while(p1&&p2) { if(p1->data<=p2->Data) {P3->next=P1; P3=p3->Next; P1=p1->Next; }Else{P3->next=P2; P3=p3->Next; P2=p2->Next; }} P3->next=p1?P1:P2; L1->next=NULL; L2->next=NULL; returnhead;}
List/Stack (the "Common Way" header node does not hold data in the way) (C language version)