Table of contents
Second, the difference between linked list and array:
2.1 Advantages and disadvantages of linked lists and arrays:
2.1.1 Advantages and disadvantages of arrays:
2.1.2 Advantages and disadvantages of linked list:
3. Static addition and dynamic traversal of the linked list:
4. Count the number of nodes in the linked list and look up the linked list
5.1 The linked list inserts a new node after the specified node:
5.2 The linked list inserts a new node from the front of the specified node:
6. Delete the specified node from the linked list
7. Change the data of the specified node in the linked list
8. Dynamically create a linked list
8.1 Dynamically create the head interpolation method of the linked list:
8.2 Tail insertion method for dynamically creating linked lists:
9. Addition, deletion, modification and query of linked list
1. The concept of linked list
1.1 What is a linked list:
-
A linked list is a data structure and an idea of data storage.
Second, the difference between linked list and array:
-
Arrays allocate memory statically, linked lists dynamically allocate memory
-
Arrays are contiguous in memory, linked lists are discontinuous
-
Arrays are positioned using subscripts, and linked lists are positioned by traversing elements
-
Array insertion and deletion need to move other elements, linked list insertion or deletion does not need to move other elements
2.1 Advantages and disadvantages of linked lists and arrays:
2.1.1 Advantages and disadvantages of arrays:
-
The random access is relatively strong, and the subscript can be used for quick positioning.
-
fast search
-
Insertion and deletion are inefficient and require moving other elements.
-
It will cause a waste of memory, because the memory is continuous, so the size of the memory must be specified when applying for an array, if it is not appropriate, it will cause a waste of memory.
-
The memory space requirement is high. To create an array, there must be enough continuous memory space.
-
The size of the array is fixed, which is specified when the array is created, and cannot be expanded dynamically.
2.1.2 Advantages and disadvantages of linked list:
-
The efficiency of insertion and deletion is high, and insertion and deletion can be performed only by changing the pointing of the pointer.
-
The memory utilization rate is high, no memory is wasted, and small discontinuous spaces in the memory can be used, and the space is only created when needed. The size is not fixed, and the expansion is very flexible.
-
The search is inefficient because the linked list traverses backwards from the first node.
#include <stdio.h> struct Test { int data; //stored data struct Test *nest; //the address of the next node }; int main() { int i; int len; int arr[3] = {1,2,3}; len = sizeof(arr)/sizeof(arr[0]); for(i=0; i<len; i++){ printf("%d ",arr[i]); } putchar('\n'); struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; t1.nest = &t2; t2.nest = &t3; printf("use t1 to print three nums\n"); printf("%d %d %d\n",t1.data,t1.nest->data,t1.nest->nest->data); return 0; } /* CLC@Embed_Learn:~/lianbiao$ gcc link.c -o link CLC@Embed_Learn:~/lianbiao$ ./link 1 2 3 use t1 to print three nums 1 2 3 */
3. Static addition and dynamic traversal of the linked list:
#include <stdio.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *hand) { struct Test *point; point = hand; //point points to the head of the linked list while(point != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",point->data); //Output the value of the current node data point = point->nest; //point points to the next node } putchar('\n'); }; int main() { //Assign values to all nodes struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; t1.nest = &t2; //Assign the address of node t2 to the nest of node t1 t2.nest = &t3; //Assign the address of node t3 to the nest of node t2 t3.nest = &t4; //Assign the address of node t4 to the nest of node t3 printf("use t1 to ptint four nums\n"); printLink(&t1); //Pass the head of the linked list to the printLink function return 0; } /* CLC@Embed_Learn:~/lianbiao$ gcc link1.c -o link1 CLC@Embed_Learn:~/lianbiao$ ./link1 use t1 to ptint four nums 1 2 3 4 */
4. Count the number of nodes in the linked list and look up the linked list
#include <stdio.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *hand) { struct Test *point; point = hand; //point points to the head of the linked list while(point != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",point->data); //Output the value of the current node data point = point->nest; //point points to the next node } putchar('\n'); } //Count the number of linked list nodes function int getLinkTotalNodeNum(struct Test *hand) { int cnt = 0; struct Test *point; point = hand; //point points to the head of the linked list while(point != NULL){ //When traversing to the tail NULL of the linked list, the loop ends cnt++; point = point->nest; //point points to the next node } return cnt; } //Find linked list function int searchLink(struct Test *hand, int data) { struct Test *point; point = hand; //point points to the head of the linked list while(point != NULL){ //When traversing to the tail NULL of the linked list, the loop ends if(point->data == data){ //If the data of the current node is the same as the input data return 1; //return 1 if found } point = point->nest; //point points to the next node } return 0; //return 0 if not found } int main() { struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; struct Test t5 = {5,NULL}; t1.nest = &t2; //Assign the address of node t2 to the nest of node t1 t2.nest = &t3; //Assign the address of node t3 to the nest of node t2 t3.nest = &t4; //Assign the address of node t4 to the nest of node t3 t4.nest = &t5; //Assign the address of node t5 to the nest of node t4 printf("Linked list dynamic output:\n"); printLink(&t1); //Pass the head of the linked list to the printLink function int ret = getLinkTotalNodeNum(&t1); printf("The number of nodes in the linked list is:%d\n",ret); ret = searchLink(&t1,1); //The function returns the final result if(ret == 0){ printf("not found 1\n"); }else{ printf("found 1\n"); } ret = searchLink(&t1,8); //The function returns the final result if(ret == 0){ printf("not found 8\n"); }else{ printf("found 8\n"); } return 0; } /* E:\code\One-stage C language\linked list>gcc demo_lb_bianli.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic output: 1 2 3 4 5 The number of nodes in the linked list is: 5 found 1 not found 8 */
Five, linked list insertion
-
There are two ways to insert into a linked list:
-
The first one: Insert a new node from behind the specified node
-
The second type: Insert a new node from the front of the specified pointer
5.1 The linked list inserts a new node after the specified node:
#include <stdio.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *hand) { struct Test *point; point = hand; //point points to the head of the linked list while(point != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",point->data); //Output the value of the current node data point = point->nest; //point points to the next node } putchar('\n'); } //Insert a new node after the specified node int insertFromBehind(struct Test *hand, int data, struct Test *new) { struct Test *p = hand; while(p != NULL){ if(p->data == data){ //Determine whether the current node is the specified node new->nest = p->nest;//Point the next node of the new node to the next node of the specified node (note that the order cannot be changed here, otherwise the linked list will be broken) p->nest = new; //Then point the next of the specified node to the new node return 1; } p = p->nest; //p points to the next node } return 0; } int main() { struct Test *hand = NULL; //Define the structure variable as a node and assign a value to the node struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; struct Test t5 = {5,NULL}; t1.nest = &t2; //Assign the address of node t2 to the nest of node t1 t2.nest = &t3; //Assign the address of node t3 to the nest of node t2 t3.nest = &t4; //Assign the address of node t4 to the nest of node t3 t4.nest = &t5; //Assign the address of node t5 to the nest of node t4 hand = &t1; struct Test new = {100,NULL}; //define a new node printf("Linked list dynamic output:\n"); printLink(hand); //Pass the head of the linked list to the printLink function int ret = insertFromBehind(hand, 3, &new); //Pass the head of the linked list, the location to be inserted, and the address of the new node if(ret == 1){ printf("Inserted successfully!\n"); }else{ printf("Insertion failed!\n"); } printLink(hand); //Pass the head of the linked list, print the linked list return 0; } /* E:\code\One-stage C language\linked list>gcc demo_insert a new node after the specified node.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic traversal: 1 2 3 4 5 Inserted successfully! 1 2 3 100 4 5 */
5.2 The linked list inserts a new node from the front of the specified node:
If you want to insert a new node before the specified node, you need to consider two situations:
-
Insert a new node before the first node
-
Insert a new node before the specified node in the middle
#include <stdio.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *hand) { struct Test *p = hand; //p points to the head of the linked list while(p != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",p->data); //Output the value of the current node data p = p->nest; //p points to the next node } putchar('\n'); } //Insert a new node in front of the specified node in the linked list struct Test* insertFromfront(struct Test *hand, int data, struct Test *new) { struct Test *p = hand; //Insert at the head node (the linked list head will change) if(p->data == data){//Determine whether the specified node is the head node new->nest = p; //Let the next of the new node point to p printf("The insertion before the first node is successful, and the head of the linked list changes!\n"); return new; //Now new is the new head of the linked list, return to the main function } //Insert before the specified node in the middle while(p->nest != NULL){ //Because it is inserted from the middle node here, it will traverse the linked list from the second node until the end of the linked list is NULL. if(p->nest->data == data){ //Determine whether the current node is the specified node new->nest = p->nest; //Let the next point of the new node to be inserted point to p->next (that is, the next node of the current node) p->nest = new; //Let the current node next point to the new node new to be inserted printf("Inserted successfully!\n"); return hand; //return the head of the linked list back to the main function } p = p->nest; //make p point to the next node } printf("Insertion failed!\n"); return hand; } int main() { struct Test *hand = NULL; //Define the structure variable as a node and assign a value to the node struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; struct Test t5 = {5,NULL}; t1.nest = &t2; //Assign the address of node t2 to the nest of node t1 t2.nest = &t3; //Assign the address of node t3 to the nest of node t2 t3.nest = &t4; //Assign the address of node t4 to the nest of node t3 t4.nest = &t5; //Assign the address of node t5 to the nest of node t4 hand = &t1; //Assign the address of the head of the linked list to hand printf("Dynamic output linked list:\n"); printLink(hand); //Pass the head of the linked list, print the linked list struct Test new = {100,NULL};//Create a new node new hand = insertFromfront(hand,1,&new);//Pass the head of the linked list, the position to be inserted, and the address of the new node new, and return the head of the linked list printf("Insert a new node before the first node:\n"); printLink(hand); struct Test new2 = {200,NULL};//Create a new node new2 hand = insertFromfront(hand,2,&new2);//Pass the head of the linked list, the position to be inserted, and the address of the new node new, and return the head of the linked list printf("Insert a new node before the specified node in the middle of the linked list:\n"); printLink(hand); return 0; } /* E:\code\One-stage C language\linked list>gcc demo_Insert a new node in front of the specified node.c E:\code\One-stage C language\linked list>a.exe Dynamic output linked list: 1 2 3 4 5 The insertion before the first node is successful, and the head of the linked list changes! Insert a new node before the first node: 100 1 2 3 4 5 Inserted successfully! Insert a new node before the specified node in the middle of the linked list: 100 1 200 2 3 4 5 */
6. Delete the specified node from the linked list
-
There are two situations in which the linked list deletes the specified node:
-
Determine whether the node to be deleted is the first node. If it is the first node, directly change the head of the linked list to make the second node the new head of the linked list
-
If the node to be deleted is not the first node, pass the nest of the previous node of the node to be deleted over the node to be deleted, and then point to the next node of the node to be deleted
-
Now the linked list is created statically. If the linked list is created dynamically, remember to release the memory of the deleted node through the free function to prevent memory leaks
#include <stdio.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *hand) { struct Test *p = hand; //p points to the head of the linked list while(p != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",p->data); //Output the value of the current node data p = p->nest; //p points to the next node } putchar('\n'); } //Linked list delete specified node struct Test* deleteNode(struct Test *hand, int data) { struct Test *p = hand; //delete the first node if(p->data == data){ //Determine whether the node to be deleted is the head node p = p->nest; //Let p point to the next node printf("The first node is successfully deleted, and the head of the linked list changes!\n"); return p; //return the new linked list head back } //delete other nodes while(p->nest != NULL){ //Start traversing the linked list from the second node if(p->nest->data == data){ //Determine whether the current node is the node to be deleted p->nest = p->nest->nest; //Pass the next of the previous node of the node to be deleted over the node to be deleted, and then point to the next node of the node to be deleted printf("Successfully deleted the specified node!\n"); return hand; //return the head of the linked list back } p = p->nest; //p points to the next node } return hand; } int main() { struct Test *hand = NULL; //Define the structure variable as a node and assign a value to the node struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; struct Test t5 = {5,NULL}; t1.nest = &t2; //Assign the address of node t2 to the nest of node t1 t2.nest = &t3; //Assign the address of node t3 to the nest of node t2 t3.nest = &t4; //Assign the address of node t4 to the nest of node t3 t4.nest = &t5; //Assign the address of node t5 to the nest of node t4 hand = &t1; //Assign the address of the head of the linked list to hand printf("Dynamic output linked list:\n"); printLink(hand); //Pass the head of the linked list, print the linked list hand = deleteNode(hand,3); //Pass the head of the linked list and the number of nodes to be deleted printf("Linked list dynamic output:\n"); printLink(hand); //Pass the head of the linked list, print the linked list return 0; } /* E:\code\One-stage C language\linked list>gcc demo_linked list delete specified node.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic output: 1 2 3 4 5 The first node is successfully deleted, and the head of the linked list changes! Linked list dynamic output: 2 3 4 5 E:\code\One-stage C language\linked list>gcc demo_linked list delete specified node.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic output: 1 2 3 4 5 Successfully deleted the specified node! Linked list dynamic output: 1 2 4 5 E:\code\One-stage C language\linked list>gcc demo_linked list delete specified node.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic output: 1 2 3 4 5 Successfully deleted the specified node! Linked list dynamic output: 1 2 3 4 E:\code\One-stage C language\linked list>gcc demo_linked list delete specified node.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic output: 1 2 3 4 5 Successfully deleted the specified node! Linked list dynamic output: 1 2 4 5 */
7. Change the data of the specified node in the linked list
#include <stdio.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *hand) { struct Test *p = hand; //p points to the head of the linked list while(p != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",p->data); //Output the value of the current node data p = p->nest; //p points to the next node } putchar('\n'); } //Change the data of the specified node in the linked list int gaiLink(struct Test *hand, int data, int newData) { struct Test *p = hand; while(p != NULL){ if(p->data == data){ //find the specified node p->data = newData; //Change the data of the specified node return 1; } p = p->nest; } return 0; } int main() { int newData; //the data to change int node; //specified node struct Test *hand = NULL; //Define the structure variable as a node and assign a value to the node struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; struct Test t5 = {5,NULL}; t1.nest = &t2; //Assign the address of node t2 to the nest of node t1 t2.nest = &t3; //Assign the address of node t3 to the nest of node t2 t3.nest = &t4; //Assign the address of node t4 to the nest of node t3 t4.nest = &t5; //Assign the address of node t5 to the nest of node t4 hand = &t1; //Assign the address of the head of the linked list to hand printf("Dynamic output linked list:\n"); printLink(hand); //Pass the head of the linked list, print the linked list printf("Please enter the data of the node you want to change:\n"); scanf("%d",&node); printf("Please enter the data you want to change:\n"); scanf("%d",&newData); int ret = gaiLink(hand,node,newData);//Pass the head of the linked list, the specified node and the data to be changed if(ret == 1){ printf("Data changed successfully!\n"); }else{ printf("Data change failed!\n"); } printf("Output linked list after data change:\n"); printLink(hand); return 0; } /* E:\code\One-stage C language\linked list>gcc demo_Change the data of the specified node of the linked list.c E:\code\One-stage C language\linked list>a.exe Linked list dynamic traversal output: 1 2 3 4 5 Please enter the data of the node you want to change: 2 Please enter the data you want to change: 100 Data changed successfully! Output linked list after data change: 1 100 3 4 5 */
8. Dynamically create a linked list
-
There are two ways to dynamically create linked lists:
-
head insertion
-
tail plugging
8.1 Dynamically create the head interpolation method of the linked list:
-
At the beginning, there was no linked list, and we need to create it dynamically: we dynamically create a new node, if the head is NULL, then let our new node be the head of the linked list, and each time the new node created is inserted before the head of the linked list, let the new node As the head of the linked list
#include <stdio.h> #include <stdlib.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *head) { struct Test *p = head; //p points to the head of the linked list while(p != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",p->data); //Output the value of the current node data p = p->nest; //p points to the next node } putchar('\n'); } //Dynamically create the head interpolation method of the linked list struct Test* insertFromHead(struct Test *head, struct Test *new) { if(head == NULL){ //when there is no linked list head = new; //The first node created is used as the head of the linked list }else{ //If there are other nodes in the linked list new->nest = head; //The nest of the new node created points to the following node head = new; //The new node acts as the head of the linked list } return head; //return list head } //Dynamically create a linked list struct Test* creatLink(struct Test *head) { struct Test *new; while(1){ new = (struct Test *)malloc(sizeof(struct Test)); //create a new node new->nest = NULL; //The nest of the new node points to NULL, otherwise an error will occur when traversing the output linked list printf("The creation of a new node is complete, please enter the data of the new node:\n"); scanf("%d",&(new->data)); //Enter data for the new node if(new->data == 0){ //Determine whether the data entered into the new node is 0 printf("0 quit\n"); free(new); //Release the node whose node data is 0 return head; //Stop creating linked list, return linked list head } head = insertFromHead(head,new); //The new node acts as the head of the linked list } return head; //return list head } int main() { struct Test *head = NULL; head = creatLink(head); //head points to the head of the linked list printf("The dynamic creation of the linked list is completed, and the linked list is dynamically output:\n"); printLink(head); struct Test t1 = {100,NULL}; head = insertFromHead(head,&t1); printf("The new node created can be inserted into the linked list by head interpolation at any time:\n"); printLink(head); return 0; } /* E:\code\One-stage C language\linked list>gcc demo_header interpolation of dynamic creation of linked list.c E:\code\One-stage C language\linked list>a.exe The creation of a new node is complete, please enter the data of the new node: 1 The creation of a new node is complete, please enter the data of the new node: 2 The creation of a new node is complete, please enter the data of the new node: 3 The creation of a new node is complete, please enter the data of the new node: 0 0 quit The dynamic creation of the linked list is completed, and the linked list is dynamically output: 3 2 1 The new node created can be inserted into the linked list by head interpolation at any time: 100 3 2 1 */
8.2 Tail insertion method for dynamically creating linked lists:
-
At the beginning, there was no linked list, and we needed to create it dynamically: we dynamically created a new node, the first node created was used as the head of the linked list, and each new node created was inserted into the nest of the last node of the linked list
#include <stdio.h> #include <stdlib.h> struct Test { int data; //data struct Test *nest; //pointer to the same type }; //Linked list dynamic traversal output void printLink(struct Test *head) { struct Test *p = head; //p points to the head of the linked list while(p != NULL){ //When traversing to the tail NULL of the linked list, the loop ends printf("%d ",p->data); //Output the value of the current node data p = p->nest; //p points to the next node } putchar('\n'); } //Dynamically create the head interpolation method of the linked list struct Test* insertBehind(struct Test *head, struct Test *new) { struct Test *p = head; if(head == NULL){ //when there is no linked list head = new; //The first node created is used as the head of the linked list return head; //return list head } while(p->nest != NULL){ //traverse to the last node p = p->nest; //p points to the next node } p->nest = new; //The new node created is inserted into the nest of the last node return head; //return list head } //Dynamically create a linked list struct Test* creatLink(struct Test *head) { struct Test *new; while(1){ new = (struct Test *)malloc(sizeof(struct Test)); //create a new node new->nest = NULL; //The nest of the new node points to NULL, otherwise an error will occur when traversing the output linked list printf("The creation of a new node is complete, please enter the data of the new node:\n"); scanf("%d",&(new->data)); //Enter data for the new node if(new->data == 0){ //Determine whether the data entered into the new node is 0 printf("0 quit\n"); free(new); //Release the node whose node data is 0 return head; //Stop creating linked list, return linked list head } head = insertBehind(head,new); //Each dynamically created new node is inserted into the linked list by tail insertion } return head; //return list head } int main() { struct Test *head = NULL; head = creatLink(head); printf("The dynamic creation of the linked list is completed, and the linked list is dynamically output:\n"); printLink(head); struct Test t1 = {100,NULL}; head = insertBehind(head,&t1); printf("The new node created can be inserted into the linked list at any time by tail insertion:\n"); printLink(head); return 0; } /* E:\code\One-stage C language\linked list>gcc demo_tail insertion method of dynamic creation of linked list.c -g E:\code\One-stage C language\linked list>a.exe The creation of a new node is complete, please enter the data of the new node: 1 The creation of a new node is complete, please enter the data of the new node: 2 The creation of a new node is complete, please enter the data of the new node: 3 The creation of a new node is complete, please enter the data of the new node: 0 0 quit The dynamic creation of the linked list is completed, and the linked list is dynamically output: 1 2 3 The new node created can be inserted into the linked list at any time by tail insertion: 1 2 3 100 */
9. Addition, deletion, modification and query of linked list
#include <stdio.h> #include <stdlib.h> struct Test { int data; struct Test *nest; }; //Dynamically traverse linked list void printLink(struct Test *head) { struct Test *p = head; while(p != NULL){ printf("%d ",p->data); p = p->nest; } putchar('\n'); } //Tail insertion method for dynamically creating linked lists struct Test* insertFromBehind(struct Test *head, struct Test *new) { struct Test *p = head; if(head == NULL){ head = new; return new; } while(p->nest != NULL){ p = p->nest; } p->nest = new; return head; } //Dynamically create a linked list struct Test* creatLink(struct Test *head) { struct Test *new; while(1){ new = (struct Test *)malloc(sizeof(struct Test)); new->nest = NULL; printf("The dynamic creation of new nodes is complete, please enter the new node data The data:\n"); scanf("%d",&(new->data)); if(new->data == 0){ printf("0 quit\n"); free(new); return head; } head = insertFromBehind(head,new); } return head; } //Count the number of linked list nodes int getLinkTotalNodeNum(struct Test *head) { struct Test *p = head; int cnt = 0; while(p != NULL){ cnt++; p = p->nest; } return cnt; } //Find linked list nodes int searchLink(struct Test *head, int data) { struct Test *p = head; while(p != NULL){ if(p->data == data){ return 1; } p = p->nest; } return 0; } //Insert a new node after the specified node int insertFromBehind2(struct Test *head, int data, struct Test *new) { struct Test *p = head; while(p != NULL){ if(p->data == data){ new->nest = p->nest; p->nest = new; return 1; } p = p->nest; } return 0; } //Insert a new node before the specified node struct Test* insertFromfront(struct Test *head, int data, struct Test *new) { struct Test *p = head; if(p->data == data){ new->nest = p; printf("on the%d Insert a new node before the node OK,The head of the linked list has changed!\n",data); return new; } while(p->nest != NULL){ if(p->nest->data == data){ new->nest = p->nest; p->nest = new; printf("at the specified node%d Insert new node in front OK\n",data); return head; } p = p->nest; } return head; } //Linked list delete specified node struct Test* deleteNode(struct Test *head, int data) { struct Test *p = head; if(p->data == data){ head = p->nest; free(p); printf("The first of the linked list%d After all nodes are deleted, the head of the linked list changes!\n",data); return head; } while(p->nest != NULL){ if(p->nest->data == data){ p->nest = p->nest->nest; printf("The first of the linked list%d Nodes have been deleted!\n",data); return head; } p = p->nest; } return head; } //Modify the data of the specified node in the linked list int gaiLink(struct Test *head, int data, int newData) { struct Test *p = head; while(p != NULL){ if(p->data == data){ p->data = newData; return 1; } p = p->nest; } return 0; } int main() { int cmd; int newData; struct Test *head = NULL; head = creatLink(head); printf("The dynamic creation of the linked list is completed, and the traversal output is as follows:\n"); printLink(head); int ret = getLinkTotalNodeNum(head); printf("The number of nodes in the linked list:%d\n",ret); printf("Please enter which node data you want to find:\n"); scanf("%d",&cmd); ret = searchLink(head,cmd); if(ret == 1){ printf("can be found:%d\n",cmd); }else{ printf("can't find%d\n",cmd); } printf("Please enter which node data you want to find:\n"); scanf("%d",&cmd); ret = searchLink(head,cmd); if(ret == 1){ printf("can be found:%d\n",cmd); }else{ printf("can't find%d\n",cmd); } struct Test new = {100,NULL}; printf("Please enter which node you want to insert after:\n"); scanf("%d",&cmd); ret = insertFromBehind2(head,cmd,&new); if(ret == 1){ printf("at the specified node%d insert new node OK\n",cmd); }else{ printf("at the specified node%d Failed to insert new node later\n",cmd); } printf("Traverse the output linked list after inserting a new node after the specified node:\n"); printLink(head); struct Test new2 = {200,NULL}; printf("Please enter which node you want to insert in front of:\n"); scanf("%d",&cmd); head = insertFromfront(head,cmd,&new2); printf("Insert a new node in front of the specified node in the linked list OK,Traverse the output:\n"); printLink(head); printf("Please enter which node you want to delete:\n"); scanf("%d",&cmd); head = deleteNode(head,cmd); printf("After the specified node is deleted from the linked list, the linked list is output:\n"); printLink(head); printf("Please enter which node you want to change data The data:\n"); scanf("%d",&cmd); printf("Please enter the data to be changed:\n"); scanf("%d",&newData); ret = gaiLink(head,cmd,newData); if(ret == 1){ printf("The data of the specified node has been modified successfully!\n"); }else{ printf("The data modification of the specified node failed!\n"); } printf("The linked list modifies the specified node data After the data, traverse the output linked list:\n"); printLink(head); return 0; } /* E:\code\One-stage C language\linked list>gcc demo_linked list.c -g E:\code\One-stage C language\linked list>a.exe The dynamic creation of new nodes is complete, please enter the data of the new node data: 1 The dynamic creation of new nodes is complete, please enter the data of the new node data: 2 The dynamic creation of new nodes is complete, please enter the data of the new node data: 3 The dynamic creation of new nodes is complete, please enter the data of the new node data: 4 The dynamic creation of new nodes is complete, please enter the data of the new node data: 5 The dynamic creation of new nodes is complete, please enter the data of the new node data: 0 0 quit The dynamic creation of the linked list is completed, and the traversal output is as follows: 1 2 3 4 5 The number of nodes in the linked list: 5 Please enter which node data you want to find: 1 Can be found in: 1 Please enter which node data you want to find: 8 can't find 8 Please enter which node you want to insert after: 5 Insert a new node after the specified node 5 OK Traverse the output linked list after inserting a new node after the specified node: 1 2 3 4 5 100 Please enter which node you want to insert in front of: 3 Insert a new node before the specified node 3 OK The linked list inserts a new node in front of the specified node OK, and traverses the output: 1 2 200 3 4 5 100 Please enter which node you want to delete: 2 The second node of the linked list is deleted! After the specified node is deleted from the linked list, the linked list is output: 1 200 3 4 5 100 Please enter the data of which node data you want to change: 200 Please enter the data to be changed: 1000 The data of the specified node has been modified successfully! After the linked list modifies the data of the specified node data, traverse the output linked list: 1 1000 3 4 5 100 */