C语言链表的一道题 30
建立一个结点按学生成绩从小到大顺序排列的链表,每个结点中包括学号,姓名和成绩。要求:1.输入一个新的学生信息,根据成绩插入到原链表中并将链表输出;2.输入一个学生的学号查...
建立一个结点按学生成绩从小到大顺序排列的链表,每个结点中包括学号,姓名和成绩。要求:1.输入一个新的学生信息,根据成绩插入到原链表中并将链表输出;2.输入一个学生的学号查找该链表中是否有学号等于X的结点.若存在,则删除该结点
展开
1个回答
展开全部
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct student *creat_list();
void print_list(struct student *head);
void query_list(struct student *head,char unumber[]);
struct student *insert_list(struct student *head,char unumber[],char uname[],char ugrade[]);
struct student *delete_list(struct student *head,char unumber[]);
struct student
{
char number[10];
char name[20];
char grade[10];
struct student *next;
};
main()
{
struct student *head;
char unumber[10];
char uname[20];
char ugrade[10];
head=creat_list();
print_list(head);
printf("input student's number for delete:\n");
scanf("%s",unumber);
head=delete_list(head,unumber);
printf("input student's number for query:");
scanf("%s",unumber);
query_list(head,unumber);
printf("input student's unumber,uname and ugrade for insert:\n");
scanf("%s %s %s",unumber,uname,ugrade);
head=insert_list(head,unumber,uname,ugrade);
printf("input student's number for delete:\n");
scanf("%s",unumber);
head=delete_list(head,unumber);
print_list(head);
}
struct student *creat_list()
{
struct student *q,*p,*head;
char unumber[10];
head=q=NULL;
while(1)
{
printf("please input student's number:");
scanf("%s",unumber);
if(strcmp(unumber,"#")==0)
break;
p=(struct student*)malloc(sizeof(struct student));
printf("please input student's name:");
scanf("%s",p->name);
printf("please input student's grade:");
scanf("%s",p->grade);
strcpy(p->number,unumber);
if(q==NULL)
head=q=p;
else
{
q->next=p;
q=p;
}
}
q->next=NULL;
return head;
}
void print_list(struct student *head)
{
struct student *p;
for(p=head;p!=NULL;p=p->next)
printf("%s,%s,%s\n",p->number,p->name,p->grade);
}
void query_list(struct student *head,char unumber[])
{
struct student *p;
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->number,unumber)==0)
break;
if(p!=NULL)
printf("student's number: %s,name:%s,grade:%s\n",unumber,p->name,p->grade);
else
printf("no this student\n");
return;
}
struct student *insert_list(struct student *head,char unumber[],char uname[],char ugrade[])
{
struct student *q,*p;
for(q=head;q!=NULL;q=q->next)
if(strcmp(q->number,unumber)==0)
break;
if(p==NULL)
{
p=(struct student *)malloc(sizeof(struct student));
strcpy(p->name,uname);
strcpy(p->number,unumber);
strcpy(p->grade,ugrade);
p->next=head;
head=p;
}
return head;
}
struct student *delete_list(struct student *head,char unumber[])
{
struct student *q,*p;
for(p=head,q=NULL;p!=NULL;q=p,p=p->next)
if(strcmp(p->number,unumber)==0)
break;
if(p!=NULL)
{
if(q==NULL)
head=p->next;
else
q->next=p->next;
free(p);
}
return head;
}
可以输入成绩,出入,插入,删除,
#include"stdlib.h"
#include"string.h"
struct student *creat_list();
void print_list(struct student *head);
void query_list(struct student *head,char unumber[]);
struct student *insert_list(struct student *head,char unumber[],char uname[],char ugrade[]);
struct student *delete_list(struct student *head,char unumber[]);
struct student
{
char number[10];
char name[20];
char grade[10];
struct student *next;
};
main()
{
struct student *head;
char unumber[10];
char uname[20];
char ugrade[10];
head=creat_list();
print_list(head);
printf("input student's number for delete:\n");
scanf("%s",unumber);
head=delete_list(head,unumber);
printf("input student's number for query:");
scanf("%s",unumber);
query_list(head,unumber);
printf("input student's unumber,uname and ugrade for insert:\n");
scanf("%s %s %s",unumber,uname,ugrade);
head=insert_list(head,unumber,uname,ugrade);
printf("input student's number for delete:\n");
scanf("%s",unumber);
head=delete_list(head,unumber);
print_list(head);
}
struct student *creat_list()
{
struct student *q,*p,*head;
char unumber[10];
head=q=NULL;
while(1)
{
printf("please input student's number:");
scanf("%s",unumber);
if(strcmp(unumber,"#")==0)
break;
p=(struct student*)malloc(sizeof(struct student));
printf("please input student's name:");
scanf("%s",p->name);
printf("please input student's grade:");
scanf("%s",p->grade);
strcpy(p->number,unumber);
if(q==NULL)
head=q=p;
else
{
q->next=p;
q=p;
}
}
q->next=NULL;
return head;
}
void print_list(struct student *head)
{
struct student *p;
for(p=head;p!=NULL;p=p->next)
printf("%s,%s,%s\n",p->number,p->name,p->grade);
}
void query_list(struct student *head,char unumber[])
{
struct student *p;
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->number,unumber)==0)
break;
if(p!=NULL)
printf("student's number: %s,name:%s,grade:%s\n",unumber,p->name,p->grade);
else
printf("no this student\n");
return;
}
struct student *insert_list(struct student *head,char unumber[],char uname[],char ugrade[])
{
struct student *q,*p;
for(q=head;q!=NULL;q=q->next)
if(strcmp(q->number,unumber)==0)
break;
if(p==NULL)
{
p=(struct student *)malloc(sizeof(struct student));
strcpy(p->name,uname);
strcpy(p->number,unumber);
strcpy(p->grade,ugrade);
p->next=head;
head=p;
}
return head;
}
struct student *delete_list(struct student *head,char unumber[])
{
struct student *q,*p;
for(p=head,q=NULL;p!=NULL;q=p,p=p->next)
if(strcmp(p->number,unumber)==0)
break;
if(p!=NULL)
{
if(q==NULL)
head=p->next;
else
q->next=p->next;
free(p);
}
return head;
}
可以输入成绩,出入,插入,删除,
追问
运行之后有错,而且没满足题目要求
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询