单链表的应用
成绩管理系统
期末考试终于考完了,可开始学习数据结构了,先准备写一个简陋的成绩管理系统来复习一下单链表的知识
需求分析
- 有基本的录入学生成绩功能
- 有基本的修改学生成绩功能
- 有基本的查找学生成绩功能
- 有基本的删除学生成绩功能
- 有基本的预览所有学生成绩功能
实现方式
- 录入学生成绩可以用头插法创建链表来实现
- 修改操作可以通过LocateElem函数找到需要修改的结点
- 查找功能通过LocateElem来实现
- 删除功能就是列表的删除操作
- 预览功能,直接用循环遍历输出就好了
功能的实现代码
/*状态码*/
#define ERROR 0
#define OK 1
typedef int Status;
/*ElemType的定义*/
typedef struct student
{
char ID[20];
char name[20];
int ProgammingGrade;
int MathGrade;
int EnglishGrade;
int sum;
double average;
};
/*链表定义*/
typedef struct node
{
struct student data;
struct node *next;
}Node;
typedef Node* LinkList;
/*输入的规范处理*/
/*字符串的输入,方便后续的程序编写*/
void StringInput(char *t,int len,char *notice)
{
char input[100];
do
{
printf("%s",notice);
scanf("%s",input);
if(strlen(input)>len)
printf("exceed the max length!\n");
}while(strlen(input)>len);
strcpy(t,input);
}
/*整型的输入,方便后续的程序编写*/
int NumberInput(char *notice)
{
int score = -1;
while (score<0 || score>100)
{
printf("%s",notice);
scanf("%d",&score);
if(score<0 || score >100)
printf("the score must in [0,100]!\n");
}
return score;
}
/*输出规范管理*/
void PrintData(Node *p)
{
//输出单个结点的数据域
printf("Student ID: %s\n",p->data.ID);
printf("Name: %s\n",p->data.name);
printf("C language score: %d\n",p->data.ProgammingGrade);
printf("English score: %d\n",p->data.EnglishGrade);
printf("Math score: %d\n",p->data.MathGrade);
printf("The total score: %d\n",p->data.sum);
printf("The average: %.2f\n",p->data.average);
printf("\n\n");
}
Status PrintALLData(LinkList l)
{
//输出整个链表的所有数据
system("clean");
Node *p;
p = l->next;
if(!p)//链表为空
{
printf("There aren't students' record.\n");
return ERROR;
}
while (p)
{
PrintData(p);
p = p->next;
}
return OK;
}
Status Add(LinkList l)
{
Node *p,*r;
char ID[20],name[20];
r=l;
while (1)
{
p = (Node*)malloc(sizeof(Node)); //为新结点申请空间
if(!p) //申请失败
{
printf("memory malloc failure!\n");
return ERROR;
}
StringInput(ID,20,"Please input student ID:(press '0' return menu)");
if(strcmp(ID,"0")==0)
break;
/*基础信息输入*/
strcpy(p->data.ID,ID);
StringInput(name,20,"Please input student name:");
strcpy(p->data.name,name);
p->data.ProgammingGrade = NumberInput("Please input C language Score[0,100]:");
p->data.MathGrade = NumberInput("Please input Math Score[0,100]:");
p->data.EnglishGrade = NumberInput("Please input English Score[0,100]:");
p->data.sum = p->data.ProgammingGrade + p->data.MathGrade + p->data.EnglishGrade;
p->data.average = (double)(p->data.sum) / 3;
/*头插法建立链表*/
p->next = NULL;
r->next = p;
r = p;
}
return OK;
}
/*
链表的LocateElem功能,稍作了些修改
对于这种查找分为两种方式
1.通过学生ID
2.通过学生name
*/
Node* LocateElem(LinkList l,char *find,char *way)
{
Node *p;
p = l->next;
/*按姓名查询*/
if(strcmp(way,"name")==0)
{
while (p)
{
if(strcmp(p->data.name,find)==0)
return p;
p = p->next;
}
}
/*按学号查询*/
else if(strcmp(way,"ID")==0)
{
while (p)
{
if(strcmp(p->data.ID,find)==0)
return p;
p = p->next;
}
}
return NULL;
}
/*查询功能*/
Status SearchStudent(LinkList l)
{
system("clear");
int choice;
char SearchInput[10];
Node *p;
p = l->next;
if(!p)
{
printf("There aren't students' record.\n");
return ERROR;
}
/*查询功能的选择界面*/
puts("1.search by ID");
puts("2.search by name");
puts("0.quit");
printf("Please input your choice(1 or 2):");
scanf("%d",&choice);
if(choice == 1)
{
StringInput(SearchInput,10,"Please input existing student ID");
//通过链表的LocateElem操作来获取查找到的结点位置
p = LocateElem(l,SearchInput,"ID");
if(!p) //找不到
{
printf("No find the record!\n");
return ERROR;
}
else
PrintData(p);
}
else if(choice == 2)
{
StringInput(SearchInput,10,"Please input existing student name");
p = LocateElem(l,SearchInput,"name");
if(!p)
{
printf("No find the record!\n");
return ERROR;
}
else
PrintData(p);
}
printf("Press any key to return\n");
getchar();
return OK;
}
/*修改功能*/
Status ModifyStduent(LinkList l)
{
system("clean");
Node *p;
p = l->next;
char SearchInput[15];
if(!p)//链表为空
{
printf("There aren't students' record.\n");
return ERROR;
}
StringInput(SearchInput,15,"Please input the existing student ID:");
p = LocateElem(l,SearchInput,"ID");//获得需要修改的结点位置
if(p)
{
PrintData(p);
StringInput(p->data.name,15,"Input new name:");
p->data.ProgammingGrade = NumberInput("C language score[0,100]: ");
p->data.MathGrade = NumberInput("Math score[0,100]: ");
p->data.EnglishGrade = NumberInput("English score[0,100]: ");
p->data.sum = p->data.ProgammingGrade + p->data.EnglishGrade + p->data.MathGrade;
p->data.average = (double)(p->data.sum)/3;
}
else
{
printf("No find the record!\n");
return ERROR;
}
return OK;
}
/*删除功能*/
Status DeleteStudent(LinkList l)
{
system("clean");
int choice;
char SearchInput[15];
Node *p,*q;
p = l->next;
if(!p)
{
printf("There aren't students' record.\n");
return ERROR;
}
//删除界面
puts("1.Delete by student ID.");
puts("2.Delete by name.");
puts("0.quit");
printf("Please input your choice: ");
scanf("%d",&choice);
if(choice == 1)
{
StringInput(SearchInput,15,"Please input the existing student ID: ");
p = LocateElem(l,SearchInput,"ID");//获得需要删除的结点位置
if(p)
{
//链表的删除操作
q = l;
while (q->next != p)
q = q->next;
q->next = p->next;
free(p);
}
else
{
printf("No find the record!\n");
return ERROR;
}
}
else if(choice == 2)
{
StringInput(SearchInput,15,"Please input the existing student name: ");
p = LocateElem(l,SearchInput,"name");
if(p)
{
q = l;
while (q->next != p)
q = q->next;
q->next = p->next;
free(p);
}
else
{
printf("No find the record!\n");
return ERROR;
}
}
printf("delete success!\n");
getchar();
return OK;
}