1. Create a single linked list
There are two common methods to dynamically establish a single linked list: head insertion method and tail insertion method.
(1) table creation by head insertion method
Start with an empty table, repeatedly read the data, generate a new node, and store the read data in the data field of the new node,
Then insert the new node into the header of the current linked list until the end flag is read. That is, each inserted node is the first node of the linked list.
1 Algorithm description: head interpolation 2 End with 32767. 3 LNode *create_LinkList(void) 4 /* The head insertion method creates a single linked list, and the head node of the linked list is used as the return value */ 5 { int data ; 6 LNode *head, *p; 7 head= (LNode *) malloc( sizeof(LNode)); 8 head->next=NULL; /* Create the header node head of the linked list */ 9 while (1) 10 { scanf("%d", &data) ; 11 if (data==32767) break ; 12 p= (LNode *)malloc(sizeof(LNode)); 13 p–>data=data; /* Data field assignment */ 14 p–>next=head–>next ; head–>next=p ; 15 /* Hook chain, the newly created node is always the first node */ 16 } 17 return (head); 18 }
(2) Table creation by tail insertion method
Although the algorithm of establishing linked list by head insertion method is simple, the order of nodes in the generated linked list is opposite to that of input.
If you want the order of the two to be the same, you can use the tail interpolation method to create the table. This method is to insert the new node into the tail of the current linked list to make it the tail node of the current linked list.
1 Algorithm description 2 LNode *create_LinkList(void) 3 /* The tail insertion method creates a single linked list, and the head node of the linked list is used as the return value */ 4 { int data ; 5 LNode *head, *p, *q; 6 head=p=(LNode *)malloc(sizeof(LNode)); 7 p->next=NULL; /* Create header node head of single linked list */ 8 while (1) 9 { scanf("%d",& data); 10 if (data==32767) break ; 11 q= (LNode *)malloc(sizeof(LNode)); 12 q–>data=data; /* Data field assignment */ 13 q–>next=p–>next; p–>next=q; p=q ; 14 /*Hook chain, the newly created node is always the last node*/ 15 } 16 return (head); 17 }
Regardless of the insertion method, if the number of nodes to be inserted into the established single linear linked list is n, the time complexity of the algorithm is O(n).
For a single linked list, no matter what kind of operation is involved, as long as it involves hooking (or re hooking)
, if no direct successor is clearly given, the order of hook chain (or re hook chain) must be "right first and then left".
2 single linked list lookup
(1) Search by sequence number to get the ith element in the single linked list.
For a single linked list, you can't directly access the node according to the sequence number i as in the sequential list, but you can only start from the head node of the linked list and search down node by node along the chain domain next until you find the ith node.
Therefore, the linked list is not a random access structure.
Let the length of the single linked list be n. to find the ith node in the table, the value of i is legal only when 1 ≤ i ≤ n.
1 Algorithm description 2 ElemType Get_Elem(LNode *L , int i) 3 { int j ; LNode *p; 4 p=L->next; j=1; /* Make p point to the first node */ 5 while (p!=NULL && j<i) 6 { p=p–>next; j++; } /* Move pointer P, J count */ 7 if (j!=i) return(-32768) ; 8 else return(p->data); 9 /* p NULL means i is too large; j> i means i is 0 */ 10 } 11 Move pointer p Frequency of: 12 i<1 Time: 0 times; i∈[1,n]: i-1 Times; i>n: n Times. 13 ∴Time complexity: O(n).
(2) Find by value
Search by value is to find whether there is a node whose node value is equal to the given value key in the linked list?
If yes, return the storage location of the node with the value of key found for the first time; Otherwise, NULL is returned. When searching, start from the start node,
Compare the node value with the given value key one by one along the linked list.
1 Algorithm description 2 LNode *Locate_Node(LNode *L,int key) 3 /* Find the first node with the value of key in the single linked list with L as the head node */ 4 { LNode *p=L–>next; 5 while ( p!=NULL&& p–>data!=key) p=p–>next; 6 if (p–>data==key) return p; 7 else 8 { printf("The node to be found does not exist!!\n"); 9 retutn(NULL); 10 } 11 } 12 Algorithm execution and formal parameters key Relevant, PingThe average time complexity is O(n).
3 insertion of single linked list
The insertion operation is to insert the new node with the value of e into the position of the ith node of the table,
That is, it is inserted between ai-1 and ai. Therefore, we must first find the node p where ai-1 is located,
Then a new node q with data field e is generated, and the q node is the direct successor node of p.
1 Algorithm description 2 void Insert_LNode(LNode *L,int i,ElemType e) 3 /* Insert a node with the value e at the ith position of the single linked list with L as the head node */ 4 { int j=0; LNode *p,*q; 5 p=L–>next ; 6 while ( p!=NULL&& j<i-1) 7 { p=p–>next; j++; } 8 9 if (j!=i-1) printf("i Too large or i Is 0!!\n "); 10 else 11 { q=(LNode *)malloc(sizeof(LNode)); 12 q–>data=e; q–>next=p–>next; 13 p–>next=q; 14 } 15 } 16 Set the length of the linked list to n,The legal insertion position is 1≦i≦n. The time of the algorithm is mainly spent moving the pointer p On, soThe time complexity is also O(n).
4 deletion of single linked list
(1) delete the ith node in the single linked list by serial number. In order to delete the ith node ai, the storage address of the node must be found.
The storage address is in the next field of its direct forward node ai-1,
Therefore, we must first find the storage location P of ai-1, and then make p – > next point to the direct successor node of ai, that is, remove ai from the chain.
Finally, free the space of node ai and return it to the "storage pool"
Algorithm description void Delete_LinkList(LNode *L, int i) /* Delete the ith node in the single linked list with L as the head node */ { int j=1; LNode *p,*q; p=L; q=L->next; while ( p->next!=NULL&& j<i) { p=q; q=q–>next; j++; } if (j!=i) printf("i Too large or i Is 0!!\n "); else { p–>next=q–>next; free(q); } }
(2) delete by value
Delete the first node with the value of key in the single linked list.
Similar to searching by value, you first need to find whether the node with the value of key exists? If it exists, delete it; Otherwise, NULL is returned.
Algorithm description void Delete_LinkList(LNode *L,int key) /* Delete the first node with the value of key in the single linked list with L as the head node */ { LNode *p=L, *q=L–>next; while ( q!=NULL&& q–>data!=key) { p=q; q=q–>next; } if (q–>data==key) { p->next=q->next; free(q); } else printf("The node to be deleted does not exist!!\n"); }
The execution of the algorithm is related to the formal parameter key, and the average time complexity is O(n).
From the above discussion, it can be seen that the insertion and deletion operations on the linked list do not need to move the node, but only need to modify the pointer.
It solves the problem that a large number of elements need to be moved for the insertion or deletion of the sequence table.
Deformation 1: delete all nodes with key in the single linked list.
Similar to search by value, but simpler than the previous algorithm.
Basic idea: start from the first node of the single linked list, check each node. If the value of the node is key, delete it, and then check the next node until all nodes are checked.
1 void Delete_LinkList_Node(LNode *L,int key) 2 /* Delete the first node with the value of key in the single linked list with L as the head node */ 3 { LNode *p=L, *q=L–>next; 4 while ( q!=NULL) 5 { if (q–>data==key) 6 { p->next=q->next; free(q); q=p->next; } 7 else 8 { p=q; q=q–>next; } 9 } 10 }
Deformation 2: delete all nodes with duplicate values in the single linked list, so that the values of all nodes are different.
Similar to search by value, but more complex than the previous algorithm.
Basic idea: starting from the first node of the single linked list, check each node: check all subsequent nodes of the node in the linked list,
Delete any value that is the same as the value of this node; Then check the next node until all nodes are checked.
1 Algorithm description 2 void Delete_Node_value(LNode *L) 3 /* Delete all nodes with the same value in the single linked list with L as the head node */ 4 { LNode *p=L->next, *q, *ptr; 5 while ( p!=NULL) /* Check all nodes in the linked list */ 6 { *q=p, *ptr=p–>next; 7 /* Check all subsequent nodes ptr of node p */ 8 while (ptr!=NULL) 9 { if (ptr–>data==p->data) 10 { q->next=ptr->next; free(ptr); 11 ptr=q->next; } 12 else { q=ptr; ptr=ptr–>next; } 13 } 14 p=p->next ; 15 } 16 }
5. Consolidation of single linked lists (just understand)
There are two ordered single linked lists whose head pointers are La and Lb respectively,
Merge them into an ordered linked list with Lc as the head pointer. The schematic diagram before merging is shown in Figure 2-4.
The schematic diagram after merging nodes with values of - 7 and - 2 is shown in Figure 2-5.
The algorithm shows that in the algorithm, pa and pb are the current nodes of the two linked lists to be investigated respectively, and pc is the last node of the merged linked list in the merging process.
1 LNode *Merge_LinkList(LNode *La, LNode *Lb) 2 /* Merge two ordered single linked lists with La and LB as the head nodes */ 3 { LNode *Lc, *pa , *pb , *pc, *ptr ; 4 Lc=La ; pc=La ; pa=La->next ; pb=Lb->next ; 5 while (pa!=NULL && pb!=NULL) 6 { if (pa->data<pb->data) 7 { pc->next=pa ; pc=pa ; pa=pa->next ; } 8 /* Merge the nodes referred to by pa and point to the next node */ 9 if (pa->data>pb->data) 10 { pc->next=pb ; pc=pb ; pb=pb->next ; } 11 /* Merge the nodes referred to by pa and point to the next node */ 12 if (pa->data==pb->data) 13 { pc->next=pa ; pc=pa ; pa=pa->next ; 14 ptr=pb ; pb=pb->next ; free(ptr) ; } 15 /* Merge the nodes indicated by pa and delete the nodes indicated by pb */ 16 } 17 if (pa!=NULL) pc->next=pa ; 18 else pc->next=pb ; /*Add the remaining nodes to the chain*/ 19 free(Lb) ; 20 return(Lc) ; 21 } 22 algorithm analysis 23 if La ,Lb The length of the two linked lists is m,n,The time complexity of linked list merging is O(m+n) .