Single linked list, the head pointer, the first node, the first node element

The first memory location in a node is called a head pointer list, the entire list must be re-access pointer began to proceed. After each node, in fact, a pointer to the subsequent position.

Here is a place to note, is to understand the concept of head pointer, this is very important. "Storage position of the first node in the list is called a head pointer ", if the chain has a head node, then the head pointer is a pointer to the head node of the data field. Draw a diagram of it.

Head pointer is the list of names. Head pointer is just a pointer to it.

  • The first node is uniform and easy to operate for the established place of the first node element, which is generally meaningless data field (of course, in some cases the length of the list may be stored, used as lookout etc.).
  • With the first node , the first element of the node before the node insertion and deletion of a node, which is unified with the operating of the other nodes.
  • Yuan first node is the first node element, it is the first node behind the head node.
  • The first node linked list is not required.
  • Yes, for the head pointer, we can also have a corresponding understanding.
  • Storage Structure linear table, linked list head pointer means is a pointer to the first node, the first node if there is a linked list, the head pointer is a pointer to the list head node.
  • Having a head pointer for identification purposes, it is commonly known as a head pointer list name.
  • Whether the list is empty, the head pointer is not empty. The head pointer is an essential element of the list.
  •  
    Single list can not head node. If there is no head node, then this will become a single list:

 

// insert here whether the head node on the list to initialize the list of knowledge
// pointer to first understand the relationship between the head and the head node: http: //www.nowamagic.net/librarys/veda/detail/1805
// define the structure node
//typedef struct LNode{
//	int data;
//	struct LNode *next;
//} LNode, * LinkList; // defines LinkList L; time, L is the head pointer list.
//
// L = (LinkList) malloc (sizeof (LNode)); // Create a node, where L is a return to the pointer, and the pointer assigned to the head.
// L-> next = null; // here that I have created a head node, that is, while the use of the head pointer and the head node.
// so easy to just add -> next to the instructions to create the head node. But you think there is no next head pointer, only the head node only, it is not difficult to understand
 
 
// take the lead node initialization
// Node * head; // declaration head node
// first first glance (* head) -> next = NULL; and we have said is not the same, as long as the head pointer to the next operation once the application automatically creates a head node
// But our focus today is not this, more that Node ** head, two hands for the operation of understanding
// pointer * a first head, the head pointer indicates that the variable is stored in another NODE pointer pointing to the structure, the second pointer, i.e. as a whole head *
// The result is a pointer which points to the structure of the content is the NODE. After such an understanding, the relationship head pointer, the first node, the first node of the yuan is very clear the
//	void InitList(Node **head){
// * head = (Node *) malloc (sizeof (Node)); // clear need here the first point, the return of all application memory address
// // The second point is (Node *) indicates that returns a pointer to the final result is a structure NODE, if it is pointing to int, then we write (int *)
//		(*head)->next=NULL;
//}
 
 
// take the lead in the end to facilitate the first node element nodes and other nodes, unified operation
	//method one:
//	void CreatList(Node **head){
// Node * r = * head, * s; // head node as previously, the head pointer is initialized, it can be used directly * head
//		int a;
//		while(scanf("%d",&a)){
//			if(a!=0){
//				s=(Node *)malloc(sizeof(Node));
//				s->value=a;
// r-> next = s; // provided there is no worry s-> next, but also because subsequent insertion of data. Thus assigned to s r
//				r=s;    
//			}
//			else{    
// r-> next = NULL; // if the subsequent input data is empty, it will be set to null
//				break;    
//			}
//		}
//}
// Call CreatList (& head); // This sentence shows that the parameter Node ** head, only the first act is the * first * No one is head and linked as a whole use
 
 
// Second way:
//	void CreatList(Node *head){
//		Node *r=head,*s;
// // The following are the same ...
//}
// Call CreatList (head);
//
 
// without head node initialization
//method one:
//void InitList(Node **head){
// * head = NULL; // Here is the direct point to the first element node, there is a misunderstanding before himself, saw head to head pointer think it is, in fact, it is just a pointer variable
         // not want it as their own before the
 
 
		// found here, the real difference whether the head node.
		// * head = (Node *) malloc (sizeof (Node)); // clear need here the first point, the return of all application memory address
		// The second point is (Node *) indicates that returns a pointer to the final result is a structure NODE, if it is pointing to int, then we write (int *)
		//(*head)->next=NULL;
//}
// Call InitList (& head);
//
// Second way:
//void InitList(Node *head){
//		head=NULL;
//}
// Call InitList (head);
 
// do not insert the lead end of the node, the first node and the other nodes operating separately
//void CreatList(Node  **head){
// Node * p, * t; / * p pointer work, t temporal 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);
// From the above fact, you know, in fact, has a head node for us, is a more sensible more convenient operation

 

First, the difference between the two:
     1, do not take the lead node is not the same as a single list for the operation with other nodes of the first node, require special handling, which increases the chance of bug complexity and the emergence of the program, therefore, usually before the start of the node singly linked list attached to a head node.
     2, First Node single list must be returned when the initial point to the head node address, so be sure to use a two-dimensional pointer (a pointer to a pointer), otherwise it will lead to memory access failure or abnormal.
This is a pointer to the point of knowledge. I do not understand the pointer to go back and look at
     3, the lead node and node initialization without head, insert, delete, output operation is not like, when traversing the linked list data output, the lead node determines that while (head-> next! = NULL ), without lead node is the while (head! = NULL), while the head pointer may be set initially, but as in claim 1, for the special case where there is only one node will have problems.
         
Second, why not take the lead node initialization There are two ways, First Node only one kind of way?
 
     Because the lead node is not declared when Node * head; C compiler automatically initialized to NULL, then no need to call InitList (head); i.e. without head node initializing a pseudo-operation. First Node initialized opened section of memory in the heap, need to modify the address (i.e., the value of head) of the head pointer variable to point, so to modify the value of the head, must pass security storage head address of the variable (i.e., a two-dimensional pointer). The direct call CreatList (head); value corresponds head pass variables, function modification is head of copies, can not really change the value of the head. 
In fact, this is the second point mentioned above content
 
Three , in fact, in essence, is passed by value, pass the problem addressed, but the pointer itself saved addresses, make this process a little tangled. When the function call to modify pointer to a variable (value), you should pass a variable address pointer (address).
      In addition, when the function parameter is a pointer, as long as the parameter is not left (i.e. both right-value operation), a two-dimensional pointer (parameters) can be simplified to a one-dimensional pointer. As above, the lead end of the insertion node simplified version.

 

Guess you like

Origin www.cnblogs.com/shujuxiong/p/10926844.html