1. Student Information
Title Description
Now I want to make up a list of student achievement points. Chain list node content includes student name, student number, grade point
The input is a list of the names, numbers, and performance points of a group of students, stored in a chain.
Delete a student node whose score is less than the average and make a new list.
Then the student information of the new list is output in the order of input.
Average Score Points are the arithmetic average of all the students'score points entered.
Enter a description
The input consists of several lines. Each line is a student's name, number, and grade point, separated by spaces.
The last line is *.
Output Description
The output includes the student's name. One line for each student's name.
#include<stdio.h> #include<string.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student { char name[20]; int num; float gpa;//Abbreviation of the English word grade point average struct Student *next; }; void main() { struct Student *head,*p1,*p2; int n=0,i=0; float sum=0,average=0; p1=p2=(struct Student *)malloc(LEN); scanf("%s%d%f",&p1->name,&p1->num,&p1->gpa); //There is no need for a space in the scanf(%s%d%f). Use spaces to indicate the input format you specified; Computers without spaces default to spaces. head=NULL; for( ; ;)//Indicates that no judgment is required, break already exists in the loop body { n=n+1; sum=sum+p1->gpa; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct Student *)malloc(LEN); scanf("%s",&p1->name); if(strcmp(p1->name,"*")==0)break; //Output ends with an asterisk else scanf("%d%f",&p1->num,&p1->gpa); } p2->next=NULL; average=sum/n; p1=p2=head; for( ;p1!=NULL; ) { if(p1->gpa>average) { p2=p1; p1=p1->next; } //Determine if the header node meets the criteria (above), change the direction of the header pointer if it does not; delete if the middle part of the list does not meet the criteria; and move the pointer when the node meets the criteria. else if(p1->gpa<average&&p1==head) { head=p1->next; free(p1);//Release memory p1=p2=head; } else if(p1->gpa<average&&p1!=head) { p2->next=p1->next; free(p1); p1=p2->next; } } p1=head; do { printf("%s\n",p1->name); p1=p1->next; }while(p1!=NULL); }
Two, One-way Chain List
Title Description
struct Node{ int data; struct Node *next; }
Programming implementation: Enter a positive integer n (0<n<10), do the following n operations: Enter several positive integers (input-1 is the end flag), create a one-way chain list, delete the odd value nodes in it, and output NULL if the chain list is empty after deletion.
Enter a description
The first row, a positive integer n, indicates that there are n groups of data;
Next n lines, each line enters several integers ending with a -1 bit flag (-1 does not belong to the sequence)
Output Description
Output chain list after deleting odd value nodes
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Node) struct Node { int data; struct Node *next; }; int number=0; void main() { void creat(int n); int n; scanf("%d",&n); creat(n); } void creat(int n) { void del(struct Node *head); void print(struct Node *head); struct Node *head,*p1,*p2; int i=0; for(i=0;i<n;i++) { head=NULL; p1=p2=(struct Node *)malloc(LEN); scanf("%d",&p1->data); for( ;p1->data!=-1; ) { number++; if(number==1)head=p1; else { p2->next=p1; p2=p1; } p1=(struct Node *)malloc(LEN); scanf("%d",&p1->data); } p2->next=NULL; del(head); number=0; } } void del(struct Node *head) { void print(struct Node *head); struct Node *p1,*p2; p1=p2=head; for( ;p1!=NULL; ) { if(p1->data%2!=0&&p1==head) { head=p1->next; free(p1); p1=p2=head; } else if(p1->data%2!=0&&p1!=head) { p2->next=p1->next; free(p1); p1=p2->next; } else if(p1->data%2==0) { p2=p1; p1=p1->next; } } print(head); } void print(struct Node *head) { struct Node *p=head; if(p==NULL)printf("NULL"); else for( ;p!=NULL; ) { printf("%d ",p->data); p=p->next; } printf("\n"); }
3. Intersection of Chain Lists
Title Description
Known as two non-descending chain list sequences S1 and S2, the design function constructs a new intersection list S3 of S1 and S2.
Enter a description
The input is divided into two rows, giving a non-descending sequence of positive integers in each row, with 1 indicating the end of the sequence (1 does not belong to this sequence). Numbers are spaced by spaces.
Output Description
Outputs the intersection sequence of two input sequences in a row, separated by spaces, with no extra spaces at the end. If the new list is empty, output NULL
#include<stdio.h> #include<stdlib.h> struct list { int data; struct list *next; }; void print(struct list *head) { struct list *p=head; if(!head) { printf("NULL"); return; } printf("%d",p->data); for(p=p->next;p!=NULL;p=p->next) printf(" %d",p->data); }//Print results struct list *creat() { int i;//i is data in a group struct list *head=NULL,*p,*q; scanf("%d",&i); if(i==-1) return head; head=(struct list *)malloc(sizeof(struct list)); head->data=i; head->next=NULL; p=head; scanf("%d",&i);//Enter each number for( ;i!=-1;scanf("%d",&i)) { q=(struct list *)malloc(sizeof(struct list)); //Second set of data q->data=i; q->next=NULL; p->next=q; p=p->next; } return head; } struct list *jiaoji(struct list *list1,struct list *list2) { int a; struct list *head=NULL,*p=list1,*q=list2,*s; if(!list1||!list2) return head; for( ;p&&q; ) { for( ;p&&q; ) { if(p->data==q->data) break; else if(p->data>q->data) q=q->next; else p=p->next; } if(!p||!q)break; if(head==NULL) { head=(struct list *)malloc(sizeof(struct list)); head->data=p->data; head->next=NULL; s=head; } else { struct list *temp=(struct list *)malloc(sizeof(struct list)); temp->data=p->data; temp->next=NULL; s->next=temp; s=s->next; temp=NULL; } a=p->data; p=p->next; q=q->next; } return head; } void main() { struct list *list1,*list2; list1=creat(); list2=creat(); print(jiaoji(list1,list2)); }
Fourth, Algorithms 2-8~2-11: Basic operations of chain tables
Title Description
Chain table is one of the most basic data structures in data structure. It is a linear table implemented with chain storage structure. It does not have to move subsequent elements when inserting and deleting, as opposed to sequential tables. Now give you some integers, then insert and delete some of them frequently, and at some point let you find an element or output all of the elements in the current list.
Here's a basic algorithm description for you:
Figure 1: Definition of chain type and algorithm description for obtaining chain elements
Figure 2: Insertion algorithm description for chain table
Figure 3: Deletion algorithm description for chain table
Figure 4: Chain table creation algorithm description
Enter a description
There is only one set of input data, the first row has n+1 integers, the first integer is the remaining number of integers in the row n, followed by n integers. This row of integers is used to initialize the list, and the order of input is the opposite of the order in the list, that is, if the list is 1, 2, 3 then the order of input is 3, 2, 1.
The second row has an integer m, which means there are m rows below. Each line has a string, one of "get", "insert", "delete", "show". If it is "get" or "delete", then an integer a follows it, representing the acquisition or deletion of the first element; If it is an "insert", then two integers a and e follow, representing the insertion of e in front of position a; There is no integer after "show".
Output Description
If successful, output the element; If the deletion succeeds, output "delete OK"; If the acquisition fails or the deletion fails, the output "get fail" and "delete fail". Output "insert OK" if the insert is successful, otherwise output "insert fail". If "show" outputs all elements in the list, and if the list is empty, outputs "Link list is empty". Note: All double quotation marks are not output.
Tips:
1. Because the input data contains a large number of insert and delete operations (believe it or not, I believe it anyway), you must use a chain list, or you will probably time out. This is also a feature of checking a chain list.
2. The elements of the initialization list are in reverse order. This is done by using the method of creating the list in the title (inserting it from the top).
#include<stdio.h> #include<stdlib.h> #include<string.h> #define LEN sizeof(struct link) struct link{ int data; struct link * next; }; struct match{ char a[20]; int data1; int data2; }item; int main() { struct link * creat(int num); void show(struct link * head); struct link * del(struct link * head,int n); struct link * insert(struct link * head,int n,int data3); void get(struct link * head,int n); struct link * head; int num,m,i; //num is the initial number of data. scanf("%d",&num); head=creat(num); scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%s",item.a); if(strcmp(item.a,"show")==0) show(head); else if(strcmp(item.a,"delete")==0) { scanf("%d",&item.data1); head=del(head,item.data1); } else if(strcmp(item.a,"insert")==0) { scanf("%d%d",&item.data1,&item.data2); head=insert(head,item.data1,item.data2); } else if(strcmp(item.a,"get")==0) { scanf("%d",&item.data1); get(head,item.data1); } } return 0; } struct link * creat(int num) { struct link * head, * p; int i; head=NULL; for(i=0;i<num;i++) { p=(struct link *)malloc(LEN); scanf("%d",&p->data); p->next=head; head=p; } return head; } void show(struct link * head) { struct link * p1=head; if(p1==NULL) printf("Link list is empty\n"); else { for(;p1!=NULL;) { printf("%d ",p1->data); p1=p1->next; } printf("\n"); } } struct link * del(struct link * head,int n) { struct link * p1, * p2; int i; int sign=0; p1=p2=head; for(i=1;p1!=NULL;i++) { if(i==n&&n==1) { sign=1; head=p1->next; free(p1); p1=p2=head; break; } else if(i==n&&n!=1) { sign=1; p2->next=p1->next; free(p1); p1=p2->next; break; } p2=p1; p1=p1->next; } if(sign) printf("delete OK\n"); else printf("delete fail\n"); return head; } struct link * insert(struct link * head,int n,int data3) { int sign=0,i; struct link * p1, * p2; p1=p2=head; if(p1==NULL&&n==1) { sign=1; p1=(struct link *)malloc(LEN); p1->data=data3; head=p1; p1->next=NULL; } else { for(i=1;p1!=NULL;i++) { if(i==n&&n==1) { sign=1; p1=(struct link *)malloc(LEN); p1->data=data3; head=p1; p1->next=p2; p2=p1; break; } else if(i==n-1&&n!=1) { sign=1; p1=(struct link *)malloc(LEN); p1->data=data3; p1->next=p2->next; p2->next=p1; p2=p1; break; } p1=p2=p1->next; } } if(sign) printf("insert OK\n"); else printf("insert fail\n"); return head; } void get(struct link * head,int n) { int i,sign=0; struct link * p1; p1=head; for(i=1;p1!=NULL;i++) { if(i==n) { sign=1; printf("%d\n",p1->data); } p1=p1->next; } }