#include <stdio.h>
#include<stdlib.h>
struct student{
char name[20];
int num;
int score;
};
struct Node{
struct student data;//自定义类型数据域data
struct Node *next;
};
struct Node *createlist()//创建链表
{
struct Node *headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
struct Node *createNode(struct student data)//创建节点
{
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printlist(struct Node *headNode)//打印链表
{
struct Node *pMove = headNode->next;
printf("name\tnum\tscore\n");
while(pMove)
{
printf("%s\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.score);
pMove = pMove->next;
}
}
void insertNodeByHead(struct Node *headNode,struct student data)//插入节点(头插法)
{
struct Node *newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppoinNum(struct Node *headNode,int num)//删除节点
{
struct Node *posNode = headNode->next;
struct Node *posNodeFront = headNode;
if(posNode == NULL)
{
printf("链表为空无法删除\n");
}
else
{
while(posNode->data.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if(posNode == NULL)
{
printf("没有找到无法删除\n");
break;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
void modifyNodeByAppoinNum(struct Node *headNode,int num)//修改节点
{
struct Node *posNode = headNode->next;
struct Node *posNodeFront = headNode;
if(posNode == NULL)
{
printf("链表为空无法修改\n");
}
else
{
while(posNode->data.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if(posNode == NULL)
{
printf("没有找到无法修改\n");
return;
}
}
printf("请输入学生姓名,学号,成绩:\n");
scanf("%s%d%d",posNode->data.name,&posNode->data.num,&posNode->data.score);
printf("修改成功!\n");
}
}
void searchNodeByAppoinNum(struct Node *headNode,int num)//查找节点
{
struct Node *posNode = headNode->next;
struct Node *posNodeFront = headNode;
if(posNode == NULL)
{
printf("链表为空\n");
}
else
{
while(posNode->data.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if(posNode == NULL)
{
printf("没有找到\n");
return;
}
}
printf("name\tnum\tscore\n");
printf("%s\t%d\t%d\n",posNode->data.name,posNode->data.num,posNode->data.score);
}
}
struct Node *DescendingByNum(struct Node *headNode)//学号降序
{
struct Node *p = headNode->next;
struct Node *pre = headNode;
struct Node *temp = NULL,*tail = NULL;
while((headNode->next->next)!=tail)
{
while(p->next != tail)
{
if((p->data.num) < (p->next->data.num))
{
pre->next = p->next;
temp = p->next->next;
p->next->next = p;
p->next = temp;
p = pre->next;
}
p = p->next;
pre = pre->next;
}
tail = p;
}
return headNode;
}
void Save(struct Node *headNode)//写入文件
{
FILE *fp = fopen("student.dat","w");
if(headNode == NULL)
return;
struct Node *pMove = headNode->next;
while(pMove)
{
fprintf(fp,"%-8s%-8s%-8s\n%-8s%-8d%-8d\n","name","num","score",pMove->data.name,pMove->data.num,pMove->data.score);
pMove = pMove->next;
}
fclose(fp);
printf("写入成功\n");
}
void Read(struct Node *headNode)//读取文件
{
FILE *fp = fopen("student.dat","r");
char ch;
if (fp == 0)
{
printf("读取失败!文件不存在\n");
return;
}
else
{
while(EOF != (ch =fgetc(fp)))
{
putchar(ch);
}
fclose(fp);
printf("读取成功!\n");
}
}
void main()
{
char choice,type;
int run = 1,searchnum;
struct Node *list = createlist();
struct student info;
system("mode con cols=100 lines=30");
printf("Management for Students' scores\n");
printf("1.Append record\n");
printf("2.List record\n");
printf("3.Delete record\n");
printf("4.Modify record\n");
printf("5.Search record\n");
printf("6.Sort Score in descending order by sum\n");
printf("7.Sort Score in ascending order by sum\n");
printf("8.Sort Score in descending order by num\n");
printf("7.Sort Score in ascending order by num\n");
printf("i.Insert Sort Score in ascending order by sum\n");
printf("w.Write to a File\n");
printf("r.Read from a File\n");
printf("0.Exit\n");
do{
printf("Please Input your choice:");
fflush(stdin);
scanf("%c",&type);
switch(type){
case '0':
run = 0;
break;
case '1':
printf("请输入学生姓名 学号 成绩\n");
scanf("%s%d%d",info.name,&info.num,&info.score);
insertNodeByHead(list,info);
while(1)
{
printf("是否继续(Y/N)?\n");
fflush(stdin);
choice = getchar();
if(choice == 'N'||choice == 'n')
break;
else
printf("请输入学生姓名 学号 成绩\n");
scanf("%s%d%d",info.name,&info.num,&info.score);
insertNodeByHead(list,info);
}
break;
case '2':
printlist(list);
break;
case '3':
printf("请输入学号用于删除:\n");
scanf("%d",&searchnum);
deleteNodeByAppoinNum(list,searchnum);
break;
case '4':
printf("请输入学号用于修改:\n");
scanf("%d",&searchnum);
modifyNodeByAppoinNum(list,searchnum);
break;
case '5':
printf("请输入学号用于检索:\n");
scanf("%d",&searchnum);
searchNodeByAppoinNum(list,searchnum);
break;
case '6':
break;
case '7':
break;
case '8':
DescendingByNum(list);
break;
case '9':
break;
case 'w':
Save(list);
break;
case 'r':
Read(list);
break;
default :
break;
}}while(run);
}