Description
cc的老师在讲完链表之后,根据9.5习题所改编的一道题目:
~~
制作一个学生管理系统,每个学生的信息有学号及成绩。 现要求实现以下功能:
~~
1. 输入
从键盘输入n个学生的数据(无确定数量,且学号应按从大到小的顺序输入)
输入0 0则输入结束**
注:n不确定 也可以为0,即只输入0 0程序也应可以运行。
2. 菜单
输入1 插入一个学生的信息,例:
若现有链表为:
1002 98.5
1006 90.3
①要插入的某学生的信息为:1001 100
结果为:
1001 100.0
1002 98.5
1006 90.3
②要插入的某学生的信息为:1003 100
结果为:
1002 98.5
1003 100.0
1006 90.3
③要插入的某学生的信息为:1010 100
结果为:
1002 98.5
1006 90.3
1010 100.0
即:通过查找学号的方式,来决定某学生的信息应插入的位置(头节点、中间节点、尾节点、)
注:实现插入功能时也应考虑到当前链表为空时的插入情况。
输入2 删除某个学生的信息,例:
**若现有链表为:**
1002 98.5
1006 90.3
**①输入 1002**
结果为:
1006 90.3
**②输入 1006**
结果为:
The List is empty!
输入0,程序停止运行。
思路分析:
应写出4个子函数:创建链表函数create,插入函数insert,删除函数del,输出函数print,用switch实现菜单功能。
下为代码:
#include <stdio.h>
#include <windows.h>
#include <malloc.h>
struct student
{
long int num;
float score;
struct student *next;
};
int n=0,LEN=sizeof(struct student);
void print(struct student *head)
{
printf("Now,these %d records are:\n",n);
struct student *p0=head;
if(head==NULL)
{
printf("The List is empty!\n");
return ;
}
printf("%ld %.1f\n",p0->num,p0->score);
while (p0->next!=NULL)
{
p0=p0->next;
printf("%ld %.1f\n",p0->num,p0->score);
}
return ;
}
struct student *create(void)
{
struct student *p1,*head=NULL;
p1=(struct student *)malloc(LEN);
p1->next=NULL;
scanf("%ld%f",&p1->num,&p1->score);
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
p1=(struct student *)malloc(LEN);
p1->next=head;
head=p1;
scanf("%ld%f",&p1->num,&p1->score);
}
if(n!=0)
head=head->next;
return head;
}
struct student *insert(struct student *head)
{
struct student *p1,*p2=head,*p3;
p1=(struct student *)malloc(LEN);
p1->next=NULL;
scanf("%ld%f",&p1->num,&p1->score);
n++;
if(head==NULL)
{
head=p1;
return head;
}
while (p1->num > p2->num && p2->next!=NULL)
{
p3=p2;
p2=p2->next;
}
if(p2==head) //头节点
{
if(p1->num<p2->num)
{
p1->next=head;
head=p1;
}
else
{
p2->next=p1;
}
}
else
{
if (p1->num > p2->num) //尾节点
{
p2->next=p1;
}
else //中间节点
{
p3->next=p1;
p1->next=p2;
}
}
return head;
}
struct student *del(struct student *head,long t)
{
struct student *p1=head,*p2;
if(head==NULL)
{
printf("The List is NULL");
return head;
}
p2=p1;
while(p1->next!=NULL&&p1->num!=t)
{
p2=p1;
p1=p1->next;
}
if(p1->num==t)
{
if(head==p1)
head=p1->next;
else
p2->next=p1->next;
free(p1);
printf("Delete succeed!");
n--;
}
else
printf("Not Found!");
return head;
}
int main()
{
int t;
long num;
struct student *p0,*head=NULL;
printf("*****Create List*****\n");
printf("Please input records(Num & Score): <0 0>for exit\n");
head=create();
printf("\n\n");
print(head);
printf("\n\n");
do
{
printf("*****MENU*****\n");
printf("1.INSERT 2.DELETE 0.EXIT\n");
printf("Please choose:");
scanf("%d",&t);
switch (t)
{
case 0:
system("pause");
return 0;
case 1:
printf("Please enter the number and score:");
head=insert(head);
printf("\n");
print(head);
printf("\n");
break;
case 2:
printf("Please enter the number:");
scanf("%ld",&num);
head=del(head,num);
printf("\n\n");
print(head);
printf("\n");
break;
}
} while (1);
system("pause");
return 0;
}
下为程序运行结果图:
此情况为输入时一个学生的信息也不录入的情况,程序也应当正常运行。
下为程序正常输入时的运行情况:
至于插入在中间节点及尾节点的情况,作者在此就不放出图片,有兴趣的人可以自己尝试一下。