本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。
函数接口定义:
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:
struct stud_node {
int num; /学号/
char name[20]; /姓名/
int score; /成绩/
struct stud_node *next; /指向下个结点的指针/
};
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
int main()
{
int min_score;
struct stud_node *p, *head = NULL;
head = createlist();
scanf("%d", &min_score);
head = deletelist(head, min_score);
for ( p = head; p != NULL; p = p->next )
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}
/* 你的代码将被嵌在这里 */
下面是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
int main()
{
int min_score;
struct stud_node *p, *head = NULL;
head = createlist();
scanf("%d", &min_score);
head = deletelist(head, min_score);
for ( p = head; p != NULL; p = p->next )
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}
struct stud_node *createlist(){
struct stud_node* head;
head=(struct stud_node *)malloc(sizeof(struct stud_node));
head->num=-1;
strcpy(head->name," ");
head->score=-1;
head->next=NULL;
struct stud_node* p=head;
while(1){
struct stud_node* new_node=(struct stud_node*)malloc(sizeof(struct stud_node));
scanf("%d",&(new_node->num));
if(new_node->num==0){
new_node->next=NULL;
break;
}
scanf("%s%d",new_node->name,&(new_node->score));
p->next=new_node;
p=new_node;
}
head=head->next;
return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score ){
struct stud_node *p,*q;
p=head;
q=NULL;
while(p!=NULL){
if(p->score<min_score&&q==NULL){
head=p->next;
free(p);
p=head;
q=NULL;
}
else{
break;
}
}
q=head;
p=head->next;
while(p!=NULL){
if(p->score<min_score){
q->next=p->next;
free(p);
p=q;
p=p->next;
}
else{
q=p;
p=p->next;
}
}
return head;
}
我在C:B里编译没有任何问题,程序也可以执行。如果能把错误提示窗口截图或许可以更好的帮助到你。
修改见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node* next;
};
struct stud_node* createlist();
struct stud_node* deletelist(struct stud_node* head, int min_score);
int main()
{
int min_score;
struct stud_node* p, * head = NULL;
head = createlist();
scanf("%d", &min_score);
head = deletelist(head, min_score);
for (p = head; p != NULL; p = p->next)
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}
struct stud_node* createlist() {
struct stud_node* head = NULL, * p;
//head = (struct stud_node*)malloc(sizeof(struct stud_node)); //修改
//head->num = -1;
//strcpy(head->name, " ");
//head->score = -1;
//head->next = NULL;
//struct stud_node* p = head;
while (1) {
struct stud_node* new_node = (struct stud_node*)malloc(sizeof(struct stud_node));
new_node->next = NULL; //修改
scanf("%d", &(new_node->num));
if (new_node->num == 0) {
//new_node->next = NULL; //修改
free(new_node); //修改
break;
}
scanf("%s%d", new_node->name, &(new_node->score));
if (head == NULL) { //修改
head = new_node; //修改
p = new_node; //修改
}
else{
p->next = new_node;
p = new_node;
}
}
//head = head->next; //修改
return head;
}
struct stud_node* deletelist(struct stud_node* head, int min_score)
{
struct stud_node* p, * q;
p = head;
q = NULL;
while (p != NULL) {
if (p->score < min_score)
{
if (q == NULL) { //修改
head = p->next;
free(p);
p = head;
q = NULL;
}
else { //修改
q->next = p->next;
free(p);
p = q;
p = p->next;
}
}
else {
//break; //修改
q = p;
p = p->next;
}
}
//q = head;
//p = head->next;
//while (p != NULL) {
// if (p->score < min_score) {
// q->next = p->next;
// free(p);
// p = q;
// p = p->next;
// }
// else {
// q = p;
// p = p->next;
// }
//}
return head;
}