"C + +" single linked list, head pointer, head node, first element nodes

Source: Internet
Author: User

The storage location of the first node in the list is called the head pointer, so the entire list must be accessed from the beginning. Each subsequent node, in fact, is the position that the previous successor pointer points to.

Here is a place to pay attention to, is the concept of the right pointer, this is very important. The storage location of the first node in the list is called the head pointer, and if the linked list has a header, then the head pointer is a pointer to the data field of the header node. Draw a picture.

The head pointer is the name of the list. The head pointer is just a pointer. The head node is set up for the unification and convenience of the operation, and before the first element node, its data field is generally meaningless (of course, in some cases, the length of the linked list, used as a monitor whistle, etc.). With the first node, the node is inserted before the first element node and the first node is deleted, which is consistent with the operation of the other nodes. The first element node is the node of the first element, which is the first node behind the head node. The header node is not required for the linked list. Yes, for the head pointer, we can also have a corresponding understanding. In the chain-type storage structure of the linear table, the head pointer is a pointer to the first node of the linked list, and if the linked list has a head node, then the head pointer is the pointer to the node of the linked table head. The head pointer has the function of marking, so the commonly used head pointer is the name of the list. The head pointer is not empty, regardless of whether the linked list is empty.   The head pointer is an essential element of a linked list. Single linked lists can also have no header nodes. If there is no head knot, then the single linked list will become like this:

Here to insert about the linked list with headless nodes to initialize the linked list knowledge//First understand the relationship between the head pointer and the head node: http://www.nowamagic.net/librarys/veda/detail/1805//define the structure of the node/
typedef struct lnode{//int data;//struct Lnode *next;//}lnode,*linklist;//defines linklist L, when L is the head pointer of the linked list. L= (linklist) malloc (sizeof (Lnode));
Creates a node where the L is returned by a pointer and assigned to the head pointer. l->next=null;
This shows that I created a head node, which uses both the head pointer and the head node. It is so convenient to add->next to the description of the creation of the head node.  But you want to think pointer is no next, only the head node only, so it is not difficult to understand//lead node initialization//node *head; Statement head node//First Sight (*head)->next=null; and we just said is not the same, as long as the head pointer once the use of next operation automatically created the head node//But our focus today is not on this, more on node **head, For operation understanding of two pointers//the first pointer *head, indicates that the head pointer variable stores another pointer to the struct node, the second pointer, the *head as a whole//the result is a pointer to the content of the struct node. After this understanding, for the head pointer, the first node, the relationship between the first element is very clear//void Initlist (node **head) {//*head= (node *) malloc (sizeof (node));//Here needs a clear 1th,
The application memory is returned with an address////2nd is (Node *) indicating that the pointer to the final result is the structure of node, and if it points to int, then we write (int *)//(*head)->next=null; The end of the lead node is convenient for the first and the other nodes, unified operation//Mode one://void Creatlist (node **head) {//node *r=*head,*s;//because the front is already the same point, the head pointer is initialized, so you can straight		Connect using *head//int A; while (scanf ("%d", &a)) {//if (a!=0) {//s= (node *) malloc (sizeof (node));//s->value=a;//r->next= s;//there is no hurry to set the S->next, because the data will also be inserted later.    
So the S is assigned to R//r=s;    
///else{//r->next=null;//set to NULL//break if subsequent input data is empty; }////Call Creatlist (&head);//This statement indicates that the form parameter node **head, only the first * is the function, the number of * is associated with the head, as the overall use of//mode two://void Crea Tlist (node *head) {//node *r=head,*s//////////////////////////////Call Creatlist (head); De **head) {//*head=null;//here is the direct point of the first element node, there is a misunderstanding before, see head feel it is a pointer, in fact it is a random pointer variable///not as I thought before///From here just found
		, there is really no difference between the head nodes. *head= (node *) malloc (sizeof (node))//This requires a clear 1th, where the application memory returns the address//2nd is (node *) indicating that its return pointer points to the final result is Node's structure, if it is pointing to int,
So let's write (int *)//(*head)->next=null; }//Call Initlist (&head);///Mode two://void initlist (node *head) {//Head=null;//}//Call Initlist (head)      The node operates separately from the other nodes//void creatlist (node **head) {//node *p,*t;   /*p work pointer, t temporary pointer//int a,i=1; while (scanf ("%d", &a)) {//if (a!=0) {//t= (node *) malloc (sizeof (node));//t->value=a;//if (i==1) {/    
/*head=t;
}//else{//p->next=t;//}//p=t;    
}//else{//p->next=null;//break;
}//i++; }////Call Creatlist (&head); in fact, from the above can know, in fact, there is a head knot for us is a more sensible and convenient operation


One, the difference between the two: 1, not the lead node of the single linked table for the first nodes of the operation and other nodes, the need for special treatment, which increases the complexity of the program and the opportunity to appear bugs, therefore,usually A header node is attached before the start node of a single linked list. 2, the first node of the single linked list, the initial must return is to point to the address of the head node, so be sure to use a two-dimensional pointer, otherwise it will lead to memory access failure or exception. This is the point of knowledge of the pointer. If you don't understand, go back and take a good look. Pointer 3, the lead node and not the lead node initialization, insert, delete, output operations are not the case, in the traversal of the output list data, the leading point of the decision condition is while (head->next!=null), and not the lead node is while (head!=           NULL), although the header pointer can be set at initial time, as described in 1, the problem arises for special cases where there is only one node. Two, why does not lead the node initialization to have 2 kinds of ways, but the lead node only has 1 kinds of ways.because no leader node declares node *head, the C compiler automatically initializes it to null, so there is no need to invoke the Initlist (head), which is not the initialization of the lead node . is a pseudo operation. And the initialization of the lead node in the heap opened up a section of memory, you need to modify the head pointer variable to the address (that is, head value), so to modify the head value, you must pass the address of the head variable (that is, the two-dimensional pointer). The direct call to Creatlist (head), which is equivalent to the value of the head variable, the function modifies the head of the copy, can not really change the head of the value. actually, that's the 2nd thing.Three, in fact, or transfer the value of the problem, but the pointer itself to save the address, let this process become a bit tangled.       When a function call needs to modify the point (value) of a pointer variable, the address of the pointer variable should be passed. In addition, a two-dimensional pointer (a formal parameter) can be reduced to a one-dimensional pointer when the parameter of a function is a pointer, as long as the argument is not on the left side (that is, all the right values operate). A simplified version of the trailing interpolation of the leading node above.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.