一道C語言題目:
試題描述
仔細閱讀下面代碼。代碼設計了單向動態鏈表,用于存盤若干個學生的學號和成績。
請完成insert函式,該函式在單向動態鏈表head中依據學號從小到大的次序插入新結點stud。
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
int n = 0;
void print(struct Student * head);
struct Student * insert(struct Student * head, struct Student * stud)
{
///程式填空,請將該函式填寫完整
}
int main()
{
struct Student *head = NULL, *stud;
int num;
float score;
while(1)
{
scanf("%d %f", &num, &score);
if(num == 0)
break;
stud = (struct Student*)malloc(LEN);
stud->num = num;
stud->score = score;
head = insert(head, stud);
}
print(head);
return 0;
}
void print(struct Student * head)
{
struct Student * p;
p = head;
if(head != NULL)
{
do{
printf("%d %.1f\n", p->num, p->score);
p = p->next;
}while(p != NULL);
}
}
注意:請務必提交完整的程式代碼,不要修改代碼框架。
輸入
輸入若干行。每行均為一個學生資訊,即學號(int范圍內的正整數)和成績(float范圍內的非負浮點數),用一個空格隔開。學生資訊用0 0結束。
輸出
依次輸出動態鏈表中各個結點中的學號和成績,用一個空格隔開。
輸入示例
1003 57
1001 67.5
1004 99
1002 86.5
0 0
輸出示例
1001 67.5
1002 86.5
1003 57.0
1004 99.0
資料范圍
輸入為int范圍的整數和float范圍的浮點數
以下是我的代碼 就是想請問Insert函式那里我總是除錯不過來 導致列印不出來東西 是哪里的問題???
求助各位前輩QWQ(按題目要求只能該動Insert函式那里)
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
int n = 0;
void print(struct Student * head);
struct Student * insert(struct Student * head, struct Student * stud)
{
///程式填空,請將該函式填寫完整
struct Student *p;
p=head;
if(p->next==NULL){
stud->next=NULL;
p->next=stud;
}
while(p){
if(p->next->num>stud->num){
stud->next=p->next;
p->next=stud;
break;
}
else{
p=p->next;
}
// if(p->next==NULL){
// stud->next=NULL;
// p->next=stud; break;
// }
}
stud->next==NULL;
}
int main()
{
struct Student *head = NULL, *stud;
int num;
float score;
while(1)
{
scanf("%d %f", &num, &score);
if(num == 0)
break;
stud = (struct Student*)malloc(LEN);
stud->num = num;
stud->score = score;
head = insert(head, stud);
}
print(head);
return 0;
}
void print(struct Student * head)
{
struct Student * p;
p = head;
if(head != NULL)
{
do{
printf("%d %.1f\n", p->num, p->score);
p = p->next;
}while(p != NULL);
}
}
uj5u.com熱心網友回復:
拜托各位前輩了 剛上大二這快學了很久還是很差勁
uj5u.com熱心網友回復:
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
int n = 0;
void print(struct Student * head);
struct Student * insert(struct Student * head, struct Student * stud)
{
///程式填空,請將該函式填寫完整
//
//
struct Student *p;
if (head == NULL) { //第一個節點
head = stud;
head->next = NULL;
return head;
}
p = head;
while (p->next) { //后面的節點~
p = p->next;
}
p->next = stud;
stud->next = NULL;
return head;
/*
struct Student *p;
p=head;
if(p->next==NULL){
stud->next=NULL;
p->next=stud;
}
while(p){
if(p->next->num>stud->num){
stud->next=p->next;
p->next=stud;
break;
}
else{
p=p->next;
}
// if(p->next==NULL){
// stud->next=NULL;
// p->next=stud; break;
// }
}
//stud->next==NULL; //注意是賦值不是判斷==
stud->next=NULL;
*/
}
int main()
{
struct Student *head = NULL, *stud;
int num;
float score;
while(1)
{
scanf("%d %f", &num, &score);
if(num == 0)
break;
stud = (struct Student*)malloc(LEN);
stud->num = num;
stud->score = score;
head = insert(head, stud);
}
print(head);
return 0;
}
void print(struct Student * head)
{
struct Student * p;
p = head;
if(head != NULL)
{
do{
printf("%d %.1f\n", p->num, p->score);
p = p->next;
}while(p != NULL);
}
}
供參考~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/11117.html
標籤:C語言
