|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
[例9.19]编程实现某班学生信息管理:学生信息包括姓名l、学号、出生日期,三门课成绩,创建一个学生信息的单向链表并输出该信息,如果班级有学生退学,根据学号删除该生信息·如果班级新进学生,根据学号将其添加到系统里.(用到链表的创建、输出、删除、添加)
代码应该如何修改 才能正确运行,或者 能写一个完整的代码吗? 谢谢大家
#include<stdio.h>
#include<stdlib.h>
struct node
{
char name[15];
int num;
struct birthday
{int year;
int month;
int day;};
struct birthday date;
float score[3];
struct node *next;
};
struct node *creat(struct node *head ,int n)
{
struct node *p, *q;
int i;
for (i=1;i<=n;i++)
{
q=(struct node *)malloc(sizeof(struct node ));
printf("Input%d姓名、学号、出生日期、三订课成绩:\n",i);
scanf("%s",q->name);
scanf("%d",&q->num);
scanf("%d%d%d",&q->date.year,&q->date.month,&q->date.day);
scanf("%f%f%f",&q->score[0],&q->score[1],&q->score[2]);
q->next=NULL;
if(head==NULL)
head=q;
else p->next=q;
p=q;
}
return (head);}
struct node *dele(struct node *head)
{
struct node *p, *q;
int x;
p=head;
printf("输入要删除的学号:\n");
scanf("%d",&x);
while(p!=NULL&&p->num!=x)
{q=p;
p=p->next;}
if(p==NULL)
printf("%d is not found!\n");
else if(p==head)
head=p->next;
else q->next=p->next;
free(p);
return(head);}
struct node *insert(struct node *head)
{ struct node *q,*p,*p1;
q= (struct node *)malloc(sizeof(struct node));
printf("Input姓名、学号、出生日期、三门课成绩:\n");
scanf("%s",q->name);
scanf("%d",&q->num);
scanf("%d%d%d",&q->date.year,&q->date.month,&q->date.day);
scanf("%f%f%f",&q->score[0],&q->score[1],&q->score[2]);
if(head==NULL)
{
q->next=NULL;
head=q;
return(head);
}
if(head->num>q->num)
{
q->next=head;
head=q;
return head;
}
p=head;
p1=head->next;
while(p1!=NULL&&p1->num<q->num)
{
p=p1;
p1=p1->next;
}
q->next=p1;
p->next=q;
return(head);}
void main()
{
struct node *creat(struct node *head,int n);
void print(struct node *head);
struct node *dele(struct node *head);
struct node *insert(struct node *head);
struct node *head=NULL;
head=creat(head,3);
print(head);
head=dele(head);
print(head);
head=insert(head);
print(head);
}
|
|