#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _node
{
char name[64];
char id[10];
char score[64];
struct _node *next;
} Node;
typedef struct _list
{
Node *head;
} List;
//--1--插入新的学生(头插法)
void AddStudentByHead(List *list);
//尾插法V1
void AddStudentByEndV1(List *list);
//尾插法V2只能单独使用
void AddStudentByEndV2(List *list);
void ChangeStudent(List *list, char *input);
void Input(Node *p);
int Search(List *list, char *input);
void Delete(List *list, char *input);
void PrintStudent(List *list);
void ClearList(List *list);
Node *PrintFlage(List *list, char *input);
int main(void)
{
List list;
list.head = NULL;
char code;
int c;
char input1[64];
char input2[64];
char input3[64];
int flag = 0;
Node *temp;
while (1)
{
printf("欢迎使用学生成绩管理系统\n");
printf("----1.插入新的学生-----\n");
printf("----2.查找已有学生----\n");
printf("----3.更改学生--------\n");
printf("----4.删除已有学生----\n");
printf("----5.显示已有学生----\n");
printf("----6.退出系统-------\n");
printf("请输入指令代码(1-6)\n");
do
{
scanf("%c", &code);
} while (code>54||code<49);
switch (code)
{
case '1':
printf("请选择插入学生的方法(1-头插法 2-尾插法V1):");
scanf("%d", &c);
switch (c)
{
case 1:
AddStudentByHead(&list);
break;
case 2:
AddStudentByEndV1(&list);
break;
default:
printf("输入指令错误\n");
}
break;
case '2':
printf("请输入你要查找的学生:");
scanf("%s", input1);
flag = (Search(&list, input1));
if (flag > 0)
{
temp = PrintFlage(&list, input1);
printf("学生姓名:%s\t学号%s\t分数:%s\t", temp->name, temp->id, temp->score);
}
else
{
printf("不好意思,查无此人\n");
}
break;
case '3':
printf("请输入你要更改的学生名字:");
scanf("%s", input2);
flag = Search(&list, input2);
temp = PrintFlage(&list, input2);
if (flag > 0)
{
printf("当前的学生信息为:\n");
printf("学生姓名:%s\t学号%s\t分数:%s\t", temp->name, temp->id, temp->score);
printf("请输入您要更改的学生信息\n");
ChangeStudent(&list, input2);
}
else
{
printf("不好意思,你要更改的学生名字尚未存在\n");
}
break;
case '4':
printf("请输入你要删除的学生名字:");
scanf("%s", input3);
if (Search(&list, input3) > 0)
{
Delete(&list, input3);
}
else
{
printf("不好意思,你要删除的学生名字尚未存在\n");
}
break;
case '5':
PrintStudent(&list);
break;
case '6':
if (!list.head)
{
printf("还没有信息呢\n");
}
goto END;
default:
printf("输入指令错误\n");
break;
}
printf("^__^\n");
printf("\n\n");
}
END:
ClearList(&list), printf("删库跑路成功^__^\n");
system("pause");
return 0;
}
//--1--插入新的学生
void AddStudentByHead(List *list)
{
Node *p, *temp;
p = (Node *)malloc(sizeof(Node));
Input(p);
if (list->head)
{
temp = list->head;
list->head = p;
p->next = temp;
}
else
{
list->head = p;
p->next = NULL;
}
}
//尾插法
void AddStudentByEndV1(List *list)
{
Node *p;
p = (Node *)malloc(sizeof(Node ));
Input(p);
Node *last;
last = list->head;
if (last)
{
while (last->next)
{
last = last->next;
}
last->next = p;
p->next=NULL;
}
else
{
list->head = p;
p->next = NULL;
}
}
//尾插法
/*
void AddStudentByEndV2(List *list)
{
Node *p;
p = (Node *)malloc(sizeof(Node ));
Input(p);
static Node *tail;
if (list->head)
{
tail=list->head;
tail->next = p;
p->next = NULL;
}
else
{
list->head = p;
p->next = NULL;
}
tail = p;
}
*/
void ChangeStudent(List *list, char *input)
{
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
Input(p);
break;
}
}
}
void Input(Node *p)
{
printf("请输入姓名:");
scanf("%s", p->name);
printf("请输入学号(2位数字):");
scanf("%s", p->id);
printf("请输入学生分数(小于1000):");
scanf("%s", p->score);
}
int Search(List *list, char *input)
{
int flag = -1; //标志
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
flag = 1;
break;
}
}
return flag;
}
Node *PrintFlage(List *list, char *input)
{
Node *p;
for (p = list->head; p; p = p->next)
{
if (!strcmp(p->name, input))
{
break;
}
}
return p;
}
void Delete(List *list, char *input)
{
Node *p, *q = NULL;
for (p = list->head; p; q = p, p = p->next)
{
if (!strcmp(p->name,input))
{
if (q)
{
q->next = p->next;
}
else
{
list->head = p->next;
}
free(p);
break;
}
}
}
void PrintStudent(List *list)
{
Node *p;
for (p = list->head; p; p = p->next)
{
printf("学生姓名:%s\t学号:%s\t分数:%s\t\n", p->name, p->id, p->score);
}
}
//--6--删库跑路
void ClearList(List *list)
{
Node *p, *q;
for (p = list->head; p; p = q)
{
q = p->next;
free(p);
}
}