C language linked list to achieve the singer rating system
- 2020-06-15 09:53:34
- OfStack
This article shares the specific code of C language linked list to achieve the singer scoring system for your reference, the specific content is as follows
This program can add file operations to save the singer score and other information, this program realized the list of the list and linked list bubble sort swap node function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Creates the structure and its members
typedef struct Node
{
int num;// Serial number
char name[20];// The name
float grade[10];// The judges scoring
float sum;// Total score
float ave;// average
struct Node *next;// Pointer to the domain
}S;// The structure is defined as S
// Function definitions
void menu(); // The menu function
S *create();// Create a linked list function
void print(S *);// Output the linked list function
void insert(S *);// Insertion node function
void del(S *);// Delete node function
void search(S *);// Lookup node function
void sort(S *);// Node sorting function
void cal(S *);// Calculate player data function
// The main function
int main()
{
S *head;
int n,a=1;//n Used to control the selection of operation types, a Control loop to -1 Termination of
while(a>0)
{
menu();// According to the menu
printf("you want to do:");
scanf("%d",&n);// Select operation
switch(n)// Each operation number corresponds to the menu number, through n Determine operation type
{
case 1:// create
head=create();
print(head);
break;
case 2:// To calculate
cal(head);
print(head);
break;
case 3:// insert
insert(head);
print(head);
break;
case 4:// delete
del(head);
print(head);
break;
case 5:// To find the
search(head);
break;
case 6:// The sorting
sort(head);
print(head);
break;
case 7:// save
save(head);
break;
case 8:
read();// read
print(head);
break;
default:
a=-1;// Break out of the loop condition
break;
}
}
return 0;
}
// Menu module is displayed directly
void menu()
{
printf("\n\n");
printf("\t\t|----------SCORE-----------|\n");
printf("\t\t|\t1.create |\n");
printf("\t\t|\t2.cal |\n");
printf("\t\t|\t3.insert |\n");
printf("\t\t|\t4.del |\n");
printf("\t\t|\t5.search |\n");
printf("\t\t|\t6.sort |\n");
printf("\t\t|\t9.exit program |\n");
printf("\t\t|--------------------------|\n");
printf("\t\t\tchoice(1-9):\n");
}
// Create the linked list module
S *create()
{
S *head,*p,*q;// Define a pointer
int i;
head=(S *)malloc(sizeof(S));// The head node opens up space
head->ave=NULL;// Short position node ave Members of the
head->next=NULL;// A pointer field that holds a null node
q=head;//q The pointer records the address of the header node
p=head->next;//p The pointer records the address of the pointer field of the header node
printf("please input singer's num and name\n");
int num;
scanf("%d",&num);
while(num!=0)// Enter contestant number enter zero to stop the loop
{
p=(S *)malloc(sizeof(S));//p Pointer opening space
// Input member
p->num=num;
scanf("%s",p->name);
printf("please input singer's score:\n");
for(i=0;i<10;i++)
{
scanf("%f",&p->grade[i]);
}
p->sum=0;
p->ave=0;
p->next=NULL;// empty p The pointer field of a node
q->next=p;//p . q The node connection
q=p;//q A pointer back
scanf("%d",&num);
}
return head;// Returns the starting address of the list
}
// Calculation module
void cal(S *head)
{
S *p;// define p Pointer to the
int i;
float max,min;// Record the highest and lowest scores
p=head->next;//p Records the pointer field address of the header node
while(p)//p Calculation is not performed for empty time
{
p->sum=0;// Initialize each computation sum
min=max=p->grade[0];// Initialize each computation min . max
for(i=0;i<10;i++)
{
p->sum+=p->grade[i];// Calculate the total score
if(p->grade[i]>max)
max=p->grade[i];// Record high score
if(p->grade[i]<min)
min=p->grade[i];// Record minimum score
}
p->ave=(p->sum-max-min)/8;// Calculate the average (remove the highest and lowest points)
p=p->next;//p A pointer back
}
}
// Insert node module (multiple inserts)
void insert(S *head)
{
int i,num,flag=1;//flag The implementation determines whether the pointer reaches the end 1 A node
float min,max;//min,max Record the highest and lowest scores
S *p,*q,*r; // Define a pointer to facilitate insertion
printf("please input a singer's messages:\n");
printf("please input singer's num:\n");
scanf("%d",&num);
while(num!=0)// When the input number is not zero, the loop terminates with zero, and multiple inserts can be realized
{
r=(S *)malloc(sizeof(S));// for r Open the space
r->next=NULL;// empty r A pointer to the domain
// Enter the relevant data and calculate the relevant data
r->num=num;
printf("please input singer's name:\n");
scanf("%s",r->name);
printf("please input singer's score:\n");
r->sum=0;
for(i=0;i<10;i++)
{
scanf("%f",&r->grade[i]);
r->sum+=r->grade[i];
}
min=max=r->grade[0];
for(i=0;i<10;i++)
{
if(r->grade[i]>max)
max=r->grade[i];
if(r->grade[i]<min)
min=r->grade[i];
}
r->ave=(r->sum-max-min)/8;
q=head;//q The pointer records the address of the header node
p=head->next;//p The pointer records the address of the pointer field of the header node
while(q->next!=NULL&&p->ave<r->ave)// Cycle condition: when q->next Don't empty , As well as p->ave<r->ave Implement insertion without sorting
{
p=p->next;//p A pointer back
q=q->next;//q A pointer back
if(q->next==NULL)// This judgment prevents q->next Is null, in the execution of the loop is a wild pointer to make the program error
{
p=NULL;// Prevent the occurrence of wild Pointers p
q->next=r;// Connect the nodes
r->next=NULL;// empty r Pointer to the domain
flag=0;// To the last 1 Node change flag
break;
}
}
if(flag)// Determine if you've reached the end 1 Nine nodes, for true to perform the operation
{
r->next=p;
q->next=r;
// Implementation will r The node inserts into the linked list
}
printf("please input singer's num:\n");
scanf("%d",&num);
}
}
// Delete node module
void del(S *head)
{
S *p,*q;// Define a pointer
int b;// Used to enter Numbers to find deletions
p=head;//p Record the address of the header node
q=head->next;//q Records the address of the pointer field of the header node
printf("input singer's num you want to delete:");
// The input number
scanf("%d",&b);
while(q!=NULL)//q The loop is executed when it is not empty
{
if(q->num==b)// Determine if the input number was found
// To true
{
p->next=q->next;// Disconnect the q node
free(q);// The release of q node neicun
q=NULL; // empty q Pointers prevent wild Pointers
}
else
{
// When judged false
p=p->next;//p A pointer back
q=q->next;//q A pointer back
}
}
if(p==NULL)// When it comes to the end 1 Output when the number of nodes to be deleted has not been checked ERROR INPUT
printf("ERROR INPUT\n");
}
// Find node module
void search(S *head)
{
S *p;// Define a pointer
int b;// define b Used to enter a lookup number
printf("input the singer's num you are searching:");
// Enter search number
scanf("%d",&b);
p=head->next;
while(p!=NULL)
{
if(p->num==b)// Determine if the contestant number has been found
{
// Is true, output information
printf("%d %s %.2f %.2f\n",p->num,p->name,p->sum,p->ave);
break;
}
else
// When is false
p=p->next;// A pointer back
}
if(p==NULL)// To the end 1 When the desired number has not been checked by the nodes, output ERROR INPUT
printf("ERROR INPUT\n");
}
// Sort node module
// Adopt bubble sort, exchange node
void sort(S *head)
{
S *p,*pre,*temp,*tail;
tail = NULL;
// The core part of the algorithm is node exchange
while( head->next != tail ){
pre= head;
p = head->next;
while( p->next != tail ){
if( p->ave > p->next->ave ){
temp = p->next;
pre->next = p->next;
p->next = p->next->next;
pre->next->next = p;
p = temp;
}
// Node (one-way
p = p->next;
pre= pre->next;
}
tail = p;
}
}
// Output linked list module
void print(S *head)
{
int i;
S *p=head->next;
while(p)// when p Execute when not empty
{
printf("%d %s %.2f %.2f\n",p->num,p->name,p->sum,p->ave);
for(i=0;i<10;i++)
printf("%.2f ",p->grade[i]);
printf("\n");
p=p->next;// A pointer back
}
}