單鏈表的應用
成績管理系統
期末考試終于考完了,可開始學習數據結構了,先準備寫一個簡陋的成績管理系統來復習一下單鏈表的知識
需求分析
- 有基本的錄入學生成績功能
- 有基本的修改學生成績功能
- 有基本的查找學生成績功能
- 有基本的刪除學生成績功能
- 有基本的預覽所有學生成績功能
實現方式
- 錄入學生成績可以用頭插法創(chuàng)建鏈表來實現
- 修改操作可以通過LocateElem函數找到需要修改的結點
- 查找功能通過LocateElem來實現
- 刪除功能就是列表的刪除操作
- 預覽功能,直接用循環(huán)遍歷輸出就好了
功能的實現代碼
/*狀態(tài)碼*/
#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;
/*輸入的規(guī)范處理*/
/*字符串的輸入号显,方便后續(xù)的程序編寫*/
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);
}
/*整型的輸入,方便后續(xù)的程序編寫*/
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;
}
/*輸出規(guī)范管理*/
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;
}