以链表为基础的学生信息小系统
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<Windows.h>
void print(struct List* phead);
void AddAtEnd(struct List** pphead, char *id,char *name,int chinese,int math,int english);
void AddAtFirst(struct List** pphead, char *id, char *name, int chinese, int math, int english);
void DeleteAtEnd(struct List** pphead);
void DeleteAtFirst(struct List** pphead);
void research(struct List* phead);
void Insert(struct List** pphead);
void DeleteAtId(struct List** pphead);
void my_sort(struct List** pphead);
void sway(struct List** tail_1, struct List** tail_2);
void method_1(struct List** pphead);
void method_2(struct List** pphead);
void title();
struct List {
char Id[20];
char Name[10];
int Chinese;
int Math;
int English;
double Average;
struct List* next;
};
int main()
{
struct List* phead = NULL;
while (1)
{
title();
int n;
scanf("%d", &n);
getchar();
switch (n)
{
case 0:
printf("确认退出? Y/N\n");
char a;
scanf("%c", &a);
if(a=='Y')
return 0;
break;
case 1:
method_1(&phead);
break;
case 2:
method_2(&phead);
break;
case 3:
DeleteAtFirst(&phead);
break;
case 4:
DeleteAtEnd(&phead);
break;
case 5:
Insert(&phead);
break;
case 6:
research(phead);
break;
case 7:
DeleteAtId(&phead);
break;
case 8:
my_sort(&phead);
break;
case 9:
print(phead);
break;
default:
printf("输入错误\n");
}
Sleep(1000);
printf("任意按键退出\n");
getchar();
getchar();
}
return 0;
}
void title()
{
printf("*****************************************************************\n");
printf("*****************************************************************\n");
printf("*** ***\n");
printf("*** ***\n");
printf("*** 学生管理链表系统 ***\n");
printf("*** ***\n");
printf("*** 头部插入 请按 1 ***\n");
printf("*** 尾部插入 请按 2 ***\n");
printf("*** 删除第一个 请按 3 ***\n");
printf("*** 删除最后一个 请按 4 ***\n");
printf("*** 在特定序号插入 请按 5 ***\n");
printf("*** 按照ID查找 请按 6 ***\n");
printf("*** 删除某个特定ID 请按 7 ***\n");
printf("*** 平均成绩排序 请按 8 ***\n");
printf("*** 输出全部 请按 9 ***\n");
printf("*** 退出 请按 0 ***\n");
printf("*** ***\n");
printf("*****************************************************************\n");
printf("*****************************************************************\n");
}
void method_1(struct List**pphead)
{
char id[20];
char name[10];
int c, m, e,num;
printf("输入几位信息:\n");
scanf("%d", &num);
printf("请以此输入学生信息:\nId\t\tName\tChinese\tMath\tEnglish\n");
for (int i = 0; i < num; i++)
{
scanf("%s %s %d %d %d", id, name, &c, &m, &e);
AddAtFirst(pphead, id, name, c, m, e);
printf("添加完成\n");
}
}
void method_2(struct List** pphead)
{
char id[20];
char name[10];
int c, m, e, num;
printf("输入几位信息:\n");
scanf("%d", &num);
printf("请以此输入学生信息:\nId\t\tName\tChinese\tMath\tEnglish\n");
for (int i = 0; i < num; i++)
{
scanf("%s %s %d %d %d", id, name, &c, &m, &e);
AddAtEnd(pphead, id, name, c, m, e);
printf("添加完成");
}
}
void print(struct List* phead) {
const char* arr[] = { "Id","Name","Chinese","Math","English","Average" };
printf("\n%-15s%-10s%-8s%-8s%-8s%s\n", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
if (phead == NULL) {
printf("NULL\n");
return;
}
while (phead->next != NULL) {
printf("%-15s%-10s%-8d%-8d%-8d%.2lf\n",phead->Id,phead->Name,phead->Chinese,phead->Math,phead->English,phead->Average);
phead = phead->next;
}
printf("%-15s%-10s%-8d%-8d%-8d%.2lf\n", phead->Id, phead->Name, phead->Chinese, phead->Math,phead->English, phead->Average);
}
void AddAtFirst(struct List** pphead, char* id, char* name, int chinese, int math, int english)
{
struct List* new_num = (struct List*)malloc(sizeof(struct List));
strcpy(new_num->Id,id);
strcpy(new_num->Name, name);
new_num->Chinese = chinese;
new_num->Math = math;
new_num->English = english;
new_num->Average = (chinese + math + english) / 3.0;
new_num->next = *pphead;
*pphead = new_num;
}
void AddAtEnd(struct List** pphead, char *id, char* name, int chinese, int math, int english)
{
struct List* tail = *pphead;
struct List* new_num = (struct List*)malloc(sizeof(struct List));
strcpy(new_num->Id, id);
strcpy(new_num->Name, name);
new_num->Chinese = chinese;
new_num->Math = math;
new_num->English = english;
new_num->Average = (chinese + math + english) / 3.0;
new_num->next = NULL;
if (*pphead == NULL) {
*pphead = new_num;
return;
}
while (tail->next != NULL)
tail = tail->next;
tail->next = new_num;
}
void DeleteAtFirst(struct List** pphead) {
if (*pphead == NULL)
return;
else if ((*pphead)->next == NULL)
{
(*pphead) == NULL;
return;
}
else
(*pphead) = (*pphead)->next;
}
void DeleteAtEnd(struct List** pphead) {
struct List* tail_1 = *pphead;
if (tail_1 == NULL)
return;
else if (tail_1->next == NULL) {
*pphead = NULL;
return;
}
else {
tail_1 = tail_1->next;
}
struct List* tail_2 = *pphead;
while (tail_1->next != NULL)
{
tail_1 = tail_1->next;
tail_2 = tail_2->next;
}
tail_2->next = NULL;
}
void Insert(struct List** pphead) {
printf("插入位置为:");
char id[20], name[10];
int chinese, math, english, loc;
scanf("%d",&loc);
printf("请以此输入学生信息:\nId\t\tName\tChinese\tMath\tEnglish\n");
scanf("%s %s %d %d %d", id, name, &chinese, &math, &english);
struct List* tail = *pphead;
struct List* new_num = (struct List*)malloc(sizeof(struct List));
strcpy(new_num->Id, id);
strcpy(new_num->Name, name);
new_num->Chinese = chinese;
new_num->Math = math;
new_num->English = english;
new_num->Average = (chinese + math + english) / 3.0;
new_num->next = NULL;
if (loc == 1) {
new_num->next = *pphead;
*pphead = new_num;
return;
}
int i = 1;
while (tail->next != NULL)
{
i++;
if (loc == i) {
new_num->next = tail->next;
tail->next = new_num;
break;
}
tail = tail->next;
}
if (1 + i == loc) {
tail->next = new_num;
return;
}
else if (loc == i)
return;
printf("对不起,插入失败,这个链表最多没有那么多元素\n");
}
void DeleteAtId(struct List** pphead)
{
printf("输入删除学生ID:\n");
char id[20];
scanf("%s", id);
struct List* tail_1 = *pphead;
if (tail_1 == NULL)
{
printf("无该id");
return;
}
else if ((tail_1->next == NULL) && (strcmp(tail_1->Id, id) == 0))
{
*pphead = NULL;
return;
}
else
tail_1 = tail_1->next;
struct List* tail_2 = *pphead;
while (tail_1->next != NULL)
{
tail_1 = tail_1->next;
tail_2 = tail_2->next;
if (strcmp(tail_1->Id, id) == 0)
{
tail_2->next = tail_1;
printf("\n删除成功:%-15s%-10s%-4d%-4d%-4d%.2lf\n", tail_1->Id, tail_1->Name, tail_1->Chinese,tail_1->Math, tail_1->English, tail_1->Average);
return ;
}
}
if (strcmp(tail_1->Id, id) == 0)
{
printf("%-15s%-10s%-4d%-4d%-4d%.2lf\n删除成功", tail_1->Id, tail_1->Name, tail_1->Chinese, tail_1->Math, tail_1->English, tail_1->Average);
tail_2->next = NULL;
}
else
printf("删除失败,无该id");
return;
}
void research(struct List* phead)
{
printf("输入查找学生ID:\n");
char id[20];
scanf("%s", id);
int tail = 0;
if (phead != NULL)
{
//除最后一位以外的遍历查找。
while (phead->next != NULL)
{
tail++;
if (strcmp(id,phead->Id)==0)
{
printf("在第%d位\n%-15s%-10s%-4d%-4d%-4d%.2lf\n",tail, phead->Id, phead->Name, phead->Chinese, phead->Math, phead->English, phead->Average);
return ;
}
phead = phead->next;
}
if (strcmp(id, phead->Id) == 0)
printf("在第%d位\n%-15s%-10s%-4d%-4d%-4d%.2lf\n", tail+1, phead->Id, phead->Name, phead->Chinese, phead->Math, phead->English, phead->Average);
}
printf("无该学生信息");
return;
}
void my_sort(struct List** pphead)
{
struct List* tail_1 = *pphead;
if ((tail_1 == NULL) || (tail_1->next == NULL))
return;
tail_1 = tail_1->next;
struct List* tail_2 = *pphead;
struct List* tail_3 = *pphead;
for (; tail_3->next != NULL;)
{
tail_2 = *pphead;
tail_1 = tail_2->next;
for (; tail_1->next != NULL;)
{
if (tail_2->Average < tail_1->Average)
sway(&tail_1, &tail_2);
tail_1 = tail_1->next;
tail_2 = tail_2->next;
}
if (tail_2->Average < tail_1->Average)
sway(&tail_1, &tail_2);
tail_3 = tail_3->next;
}
}
void sway(struct List** tail_1, struct List** tail_2)
{
char str[20];
strcpy(str, (*tail_1)->Id);
strcpy((*tail_1)->Id, (*tail_2)->Id);
strcpy((*tail_2)->Id, str);
strcpy(str, (*tail_1)->Name);
strcpy((*tail_1)->Name, (*tail_2)->Name);
strcpy((*tail_2)->Name, str);
int x = (*tail_1)->Chinese;
(*tail_1)->Chinese = (*tail_2)->Chinese;
(*tail_2)->Chinese = x;
x = (*tail_1)->Math;
(*tail_1)->Math = (*tail_2)->Math;
(*tail_2)->Math = x;
x = (*tail_1)->English;
(*tail_1)->English = (*tail_2)->English;
(*tail_2)->English = x;
double b= (*tail_1)->Average;
(*tail_1)->Average = (*tail_2)->Average;
(*tail_2)->Average = b;
}