Linked list is a common data structure. It is different from common arrays. When using an array, you must first specify the number of elements contained in the array, that is, the length of the array. However, if the elements added to the array exceed the size of the array, you cannot save all the contents.
the number of elements in the linked list storage method is unlimited. When adding elements, the number of stored elements will change accordingly.
First, initialize the linked list
struct Node { int data; struct Node *next; };
If the linked list of student scores is operated, the structure nesting function can be used
struct student { char name[20]; int num; int Chi; //language int math; int English; }; //Linked list initialization struct Node { struct student data; //Data domain struct Node *next; //Pointer field };
Next, create a linked list (create a header node)
struct Node *createlist() { struct Node *headNode = (struct Node*)malloc(sizeof(struct Node)); //Initialization of variables before use headNode->next = NULL; return headNode; };
After creating the header node, insert it next
The first method: head insertion
void insertNodeByHead(struct Node*headNode,struct student data)/*Head insertion*/ { //Create inserted node struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = headNode->next; headNode->next = newNode; }
The second method: tail interpolation The difference from the head insertion method is that you need to traverse the whole linked list to the last one first, and the rest is the same as the head insertion method
//Tail interpolation void insertNodeByEnd(struct Node *headNode,struct student data) { struct Node *temp =headNode; //Same address while(temp->next ) { temp = temp->next ; } if(temp) { struct Node *s = (struct Node*)malloc(sizeof(struct Node)); //s->next = NULL; temp->next = s; temp = s; strcpy(s->data.name,data.name); s->data.num = data.num ; s->data.math = data.math ; temp->next = NULL; } }
The third method: random position insertion. Compared with the previous two methods, the third method is more flexible and can freely choose the insertion position
//Insert anywhere void inserteveryNode(struct Node*headNode,int p/*Where to insert*/,struct student data) { int i; struct Node *w = headNode; struct Node *s = (struct Node*)malloc(sizeof(struct Node)); for(i=0;i<p;i++) { w = w->next ; if(w == NULL) { printf("error"); return; } } strcpy(s->data.name,data.name); s->data.num = data.num; s->data.Chi = data.Chi; //Chi is Chinese s->data.math = data.math; s->data.English = data.English; s->next = w->next; w->next = s; }
Then delete
//Delete operation void deleteNodeByAppion(struct Node*headNode ,int num) { struct Node*posNode = headNode->next; struct Node*posNodefront = headNode; if(posNode==NULL) printf("Cannot delete the linked list. It is empty\n"); else { while(posNode->data.num != num) { posNodefront = posNode ; posNode = posNodefront->next; if(posNode==NULL) { printf("No relevant information was found and cannot be deleted\n"); return; } } posNodefront->next = posNode->next; free(posNode); } }
Then modify
This code can perform various functions and freely select the data you want to modify
//Modify operation struct Node *gai(struct Node*headNode,int count,char w) { //struct student data; int Chi,English,math,num; char name[20]; struct Node *p = headNode; int j; for(j=0;j<count;j++) { p = p->next ; } if(w == 'A') { printf("Please enter the modified student name: "); scanf("%s",name); strcpy(p->data.name,name); } if(w == 'B') { printf("Please enter the modified student number: "); scanf("%d",&num); p->data.num = num; } if(w == 'C') { printf("Please enter the revised student language score: "); scanf("%d",&Chi); p->data.Chi = Chi; } if(w == 'D') { printf("Please enter the revised student math score: "); scanf("%d",&math); p->data.math = math; } if(w == 'E') { printf("Please enter the revised student English score: "); scanf("%d",&English); p->data.English = English; } return p; }
The following is the simplified code (this code modifies the student number (num))
You can also freely change the formal parameters to adjust the changed data
//Modify operation struct Node *gai(struct Node*headNode,int count,int num) { //struct student data; int Chi,English,math,num; char name[20]; struct Node *p = headNode; int j; for(j=0;j<count;j++) { p = p->next ; } p->data.num = num; return p; }
Finally, perform the search operation
struct Node *cha2(struct Node*headNode,int i) { struct Node *k = headNode; int j; for(j=0;j<i;j++) { k = k->next; } return k; }
Enter a sequence number to be searched (i) search with the help of circular statements, and finally return the data saved by the whole node
Find the length of the linked list
//Find length int len(struct Node *headNode) { int len = 0; struct Node *p = headNode->next; while(p) { p=p->next; len++; } return len; }
Finally, sort the linked list
As like as two peas, the sorting of the list is exactly the same as that of the normal array.
The sorting method is described below:
1. Bubble sorting
//List bubble sort void maopao(struct Node *headNode) { char name1[20]; int math_; struct Node *p ; struct Node *q ; struct Node *t ; for(p = headNode;p->next != NULL;p = p->next) { for(q = headNode;q->next != NULL;q = q->next ) { if(q->data.num < q->next->data.num) { int temp = q->data.num; q->data.num = q->next->data.num; q->next->data.num = temp; strcpy(name1,q->data.name); strcpy(q->data.name,q->next->data.name); strcpy(q->next->data.name,name1); math_ = q->data.math; q->data.math = q->next->data.math; q->next->data.math = math_; } } } }
2. Select Sorting
void maopao(struct Node *headNode) { struct Node *p, *s; char name1[20]; int math_; for(s = headNode->next;s!=NULL;s = s->next) { for(p = s->next;p!=NULL;p = p->next) { if(p->data.num > s->data.num) { int temp = p->data.num; p->data.num = s->data.num; s->data.num = temp; strcpy(name1,p->data.name); strcpy(p->data.name,s->data.name); strcpy(s->data.name,name1); math_ = p->data.math ; p->data.math = s->data.math; s->data.math = math_; } } } }
Finally, traverse the linked list
//Traversal linked list void printfList(struct Node*headNode){ struct Node*pMove = headNode->next; printf("name\tnum\tChi\tmath\tEnglish\n"); while(pMove) { printf("%s\t%d\t%d\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.Chi,pMove->data.math,pMove->data.English); pMove = pMove->next; } printf("\n"); }
The complete code is as follows:
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<string.h> int count = 1; struct student { char name[20]; int num; int Chi; int math; int English; }; //Linked list initialization struct Node { struct student data; //Data domain struct Node *next; //Pointer field }; //Create header linked list struct Node *createlist() { struct Node *headNode = (struct Node*)malloc(sizeof(struct Node)); //Initialization of variables before use headNode->next = NULL; return headNode; }; //Create header node /*struct Node*createNode(struct student data){ struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; };*/ //Traversal linked list void printfList(struct Node*headNode){ struct Node*pMove = headNode->next; printf("name\tnum\tChi\tmath\tEnglish\n"); while(pMove) { printf("%s\t%d\t%d\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.Chi,pMove->data.math,pMove->data.English); pMove = pMove->next; } printf("\n"); } //Head insertion void insertNodeByHead(struct Node*headNode,struct student data)/*Head insertion*/ { //Create inserted node struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); /*newNode->data.English = data.English; newNode->data.math = data.math; newNode->data.Chi = data.Chi; newNode->data.num = data.num; strcpy(newNode->data.name,data.name);*/ newNode->data = data; newNode->next = headNode->next; headNode->next = newNode; } //Tail interpolation /*void insertNodeByEnd(struct Node *headNode,struct student data) { struct Node *temp =headNode;//Same address while(temp->next ) { temp = temp->next ; } if(temp) { struct Node *s = (struct Node*)malloc(sizeof(struct Node)); //s->next = NULL; temp->next = s; temp = s; strcpy(s->data.name,data.name); s->data.num = data.num ; s->data.math = data.math ; temp->next = NULL; } }*/ //Insert anywhere /*void inserteveryNode(struct Node*headNode,int p/*Where to insert*//*,struct student data)*/ /*{ int i; struct Node *w = headNode; struct Node *s = (struct Node*)malloc(sizeof(struct Node)); for(i=0;i<p;i++) { w = w->next ; if(w == NULL) { printf("error"); return; } } strcpy(s->data.name,data.name); s->data.num = data.num; s->data.Chi = data.Chi; s->data.math = data.math; s->data.English = data.English; s->next = w->next; w->next = s; }*/ //Delete operation void deleteNodeByAppion(struct Node*headNode ,int num){ struct Node*posNode = headNode->next; struct Node*posNodefront = headNode; if(posNode==NULL) printf("Cannot delete the linked list. It is empty\n"); else { while(posNode->data.num != num) { posNodefront = posNode ; posNode = posNodefront->next; if(posNode==NULL) { printf("No relevant information was found and cannot be deleted\n"); return; } } posNodefront->next = posNode->next; free(posNode); } } //Find operation /*void cha(struct Node *headNode,int num) { // printf("dasdasdasdas"); //Note Ono (wild pointer) struct Node*p = headNode->next; int count = 0; while(p) { count++; if(p->data.num == num) { printf("%d %s %d %d",count,p->data.name ,p->data.num ,p->data.math ); return; } p = p->next; } printf("'); not found; return; }*/ //The second method of checking struct Node *cha2(struct Node*headNode,int i) { struct Node *k = headNode; int j; for(j=0;j<i;j++) { k = k->next; } return k; } //Modify operation struct Node *gai(struct Node*headNode,int count,char w) { //struct student data; int Chi,English,math,num; char name[20]; struct Node *p = headNode; int j; for(j=0;j<count;j++) { p = p->next ; } if(w == 'A') { printf("Please enter the modified student name: "); scanf("%s",name); strcpy(p->data.name,name); } if(w == 'B') { printf("Please enter the modified student number: "); scanf("%d",&num); p->data.num = num; } if(w == 'C') { printf("Please enter the revised student language score: "); scanf("%d",&Chi); p->data.Chi = Chi; } if(w == 'D') { printf("Please enter the revised student math score: "); scanf("%d",&math); p->data.math = math; } if(w == 'E') { printf("Please enter the revised student English score: "); scanf("%d",&English); p->data.English = English; } return p; } //Find length int len(struct Node *headNode) { int len = 0; struct Node *p = headNode->next; while(p) { p=p->next; len++; } return len; } //Linked list sorting 1.0 /*void maopao(struct Node *headNode) { struct Node *p, *s ; char name1[10]; int math_; for(s = headNode->next;s!=NULL;s = s->next) { for(p = s->next;p!=NULL;p = p->next) { if(p->data.num > s->data.num) { int temp = p->data.num; p->data.num = s->data.num; s->data.num = temp; strcpy(name1,p->data.name); strcpy(p->data.name,s->data.name); strcpy(s->data.name,name1); math_ = p->data.math ; p->data.math = s->data.math; s->data.math = math_; } } } }*/ //List bubble sort void maopao(struct Node *headNode) { char name1[20]; int math_; struct Node *p ; struct Node *q ; struct Node *t ; for(p = headNode;p->next != NULL;p = p->next) { for(q = headNode;q->next != NULL;q = q->next ) { if(q->data.num < q->next->data.num) { int temp = q->data.num; q->data.num = q->next->data.num; q->next->data.num = temp; strcpy(name1,q->data.name); strcpy(q->data.name,q->next->data.name); strcpy(q->next->data.name,name1); math_ = q->data.math; q->data.math = q->next->data.math; q->next->data.math = math_; } } } } int main() { struct Node* List = createlist(); struct student info; while(1) { printf("Please enter the student's name, student number, Chinese score, math score, English score: \n"); scanf("%s",info.name); scanf("%d %d %d %d",&info.num ,&info.Chi ,&info.math,&info.English); insertNodeByHead(List,info); //insertNodeByEnd(List,info); printf("continue(Y/N)?\n"); getchar(); char choice ; //setbuf(stdin,NULL); / / clear the buffer. / / there is a problem between the buffer and the file stream// scanf("%c",&choice); if(choice == 'N'|| choice == 'n') { //printf("exit output"); break; } } printfList(List); printf("Sort it out \n"); maopao(List); printfList(List); /*printf("Please enter the student's name, student number and math score: \ n ""); scanf("%s %d %d",info.name ,&info.num ,&info.math ); int l; //Serial number of the group printf("Please select where to insert data ""); scanf("%d",&l); inserteveryNode(List,l,info); printfList(List);*/ printf("Please enter the student ID you want to delete:\n"); int num; scanf("%d",&num ); deleteNodeByAppion(List,num ); printfList(List); /*printf("Please enter the data you want to find: \ n ""); int math ; scanf("%d",&math ); cha(List, math); cha printf("\n");*/ int people; printf("Which student do you want to find"); scanf("%d",&people); struct Node *m = cha2(List,people); /* cha2 */ printf("The student's name, student number, Chinese score, math score, English score: \n"); printf("%s\t%d\t%d\t%d\t%d\n",m->data.name ,m->data.num ,m->data.Chi,m->data.math,m->data.English); printf("\n"); int lenss; printf("Please select which student's relevant information to change:\n"); scanf("%d",&lenss); printf("Please select the information you want to change:\n"); printf("A.full name B.Student number C.grade scores of Chinese D.Mathematics achievement E.English achievement \n"); //setbuf(stdin,NULL); // It is clear that the cache can not be used casually getchar(); char t = getchar(); struct Node* nums = gai(List,lenss,t); printf("\n"); printf("The student's changed information:\n"); //setbuf(stdin,NULL); printf("%s %d %d %d %d\n",nums->data.name ,nums->data.num ,nums->data.Chi,nums->data.math,nums->data.English ); int lens = len(List); printf("The length of the single linked list is:%d\n",lens); system("pause"); return 0; }