current position:Home>Dynamic linked list failed to use bubble sorting
Dynamic linked list failed to use bubble sorting
2022-02-02 22:48:14 【CSDN Q & A】
Create dynamic linked list , Enter the corresponding score and sort according to the score , But when you enter more than two sets of data, the program crashes , Ask for an answer
#include"stdio.h"#include"string.h"#include"stdlib.h"struct student{ int id; char name[20]; float score; struct student *next;};void order(struct student a[10]);int i,n,j;float m;int main(){ struct student *p1,*p2,*head; struct student a[10]; printf(" Enter the number of players :\n"); scanf("%d",&n);getchar(); printf(" Enter the name of each player id Number 、 Name and score :\n"); p1=p2=(struct student*)malloc(sizeof(struct student)); for(i=0;i<n;i++) { printf("Input id and name:\n"); scanf("%d %s",&p2->id,&p2->name); printf("Input 5 scores:\n"); p2->score=0; for(j=0;j<5;j++) { scanf("%f",&m); p2->score+=m/5.0; } if(i==0){
head=p2;head->next=NULL;} else { head->next=p2; p2=p2->next; } p2=(struct student*)malloc(sizeof(struct student)); } p2->next=NULL; for(i=0;i<n;i++) { a[i].id=p1->id; strcpy(a[i].name,p1->name); a[i].score=p1->score; p1=p1->next; } order(a); printf(" Ranking and average score per person :\n"); for(i=0;i<n;i++) printf("%d %s %.2f\n",a[i].id,a[i].name,a[i].score);}void order(struct student a[10]){ struct student w;w.next=NULL; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) { if(a[i].score<a[j].score) { w.id=a[i].id; strcpy(w.name,a[i].name); w.score=a[i].score; a[i].id=a[j].id; strcpy(a[i].name,a[j].name); a[i].score=a[j].score; a[j].id=w.id; strcpy(a[j].name,w.name); a[j].score=w.score; } }}
The input data is 3 and 2 The result of time :
Expected results :
Hope to answer T^T
Refer to the answer 1:
I don't know why you have p1,p2,2 A pointer to the , What are you doing
Before sorting , Print your own list first , See what's in the bottom
Refer to the answer 2:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022248112718.html
The sidebar is recommended
- Create a string variable
- This cycle makes me a little confused
- Why does RGB of Python line chart run in scarlet letter?
- Library management system, how to use two arrays together
- How to download files to browser through PHP FTP
- Why is the range of the second for loop < I-1?
- What should I fill in the air room?
- 0: {key1: value1, key2: Value2} what kind of data is this
- Can I access UI components in Python multi process?
- Enter N and n positive integers and output all prime numbers. If not, output No
guess what you like
-
Spring project deployment Tomcat
-
How to use Python to implement a hash table
-
The command prompt cannot be displayed as you look
-
Solve, why can't this work
-
Want to know F4 what's wrong
-
HTML + PHP + Python web page displays Chinese garbled code
-
Introduction to C language: pointer and multidimensional array
-
C language can judge whether the password is correct and return whether it is correct, but there is no way to judge after the password is wrong
-
How does the core MVC razorpages page access the backend method?
-
How to design test papers freely according to your own needs in HTML
Random recommended
- Dlib is already in setting, but there will still be errors when running
- Vs2015 migration project, failed to connect can
- www.bing. Com took too long to respond
- Why do computers use complement codes
- Why do floating point numbers lose accuracy? How to understand
- The problem of Vue routing history mode jumping to a new window
- PR how to remove the original video subtitles
- How do I get a set of filtered drop-down items in an excel table?
- How to set to allow the WWW service to pass through the server (port 80)
- What are the recording chips with built-in flash? It can record about 40 seconds. What are the characteristics of the chip?
- The company has added a batch of computers. Now it needs to add a network segment to divide the new computers into this network segment
- Jenkins deployment project execution pipeline script prompt: Python module not found
- Do you know how to solve & lt/ details&gt; Too many child elements in the page will cause the page to jam when sliding
- Which database does the following text image come from? Does anyone know the original image and GT image?
- How to install PIP? I have found many ways to install it, as shown in the figure
- It's a little difficult for you to find mistakes. All kinds of mistakes
- MySQL sub nested data update
- How do I get the words on the console to the window?
- How to extract specified data from text in C language
- Failed to load file or assembly
- How does git handle this situation
- How does Python prevent multiple logs under the same file from writing to one file at the same time
- 174 allocation treasure binary violence solution error
- How does Calc (x - y) in CSS make y the height of a class
- How does CSDN mask messages
- About javascript: iframe web page B is embedded. There is a form in web page B, and the parent Web page a does not refresh
- Wechat applet authorization login problem
- C language, enter 10 integers, find the average and fill in the blank
- The linked list code in cFree can run, but the memory limit is exceeded when submitting
- Oracle database startup failed. The data file header is damaged
- In EF core, how to create a sequence and set nextvalue as the default value when defining the ID of a table?
- timing npm:load:cleanupLog Completed in 2ms
- Esp8266 failed to connect to the serial port, indicating that the serial port is occupied or does not exist
- It's strange why the output is - 1
- Here's why getchar can be executed. After it becomes scanf, even if the correct value is entered, it still prompts for input error
- Is there a question about monotonous station
- Using C + + to complete different N / I applets
- What's the matter with API
- Canvas cannot load picture Tkinter
- If the HTML page cannot be displayed normally, the code of {% block head%} {% endblock%} will be displayed directly. I don't know what the problem is