(作者:KX、DavidZhang)
我的版本:
(code:C)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct STU
{
char name[10];
int id;
int age;
char address[50];
struct STU *next;
};
struct STU* creatList();
struct STU* getStuList();
void putStuList(struct STU*);
void searchStu(struct STU*,char);
void insertStu(struct STU*);
void deleteStu(struct STU*);
int countStu(struct STU*);
void tapToContinue(char*);
int main()
{
struct STU *head=NULL;
char command;
do
{
int stuAmount=countStu(head);
printf("共%d个学生:\n",stuAmount);
printf("┏━━━━━┓\n");
printf("┃请输入命令┃\n");
printf("┣━━━━━┫\n");
if(head!=NULL)
{
printf("┃1.查询学生┃\n");
printf("┃2.添加学生┃\n");
printf("┃3.删除学生┃\n");
printf("┃4.打印列表┃\n");
printf("┃5.保存列表┃\n");
}
printf("┃6.新建列表┃\n");
printf("┃7.读取列表┃\n");
printf("┃0.退出系统┃\n");
printf("┗━━━━━┛\n");
command=getch();
switch(command)
{
case'1':
searchStu(head,'0');
break;
case'2':
insertStu(head);
break;
case'3':
searchStu(head,'A');
deleteStu(head);
tapToContinue("");
break;
case'4':
searchStu(head,'A');
tapToContinue("");
break;
case'5':
putStuList(head);
tapToContinue("");
break;
case'6':
head=creatList();
tapToContinue("");
break;
case'7':
head=getStuList();
tapToContinue("");
break;
case'0':
system("exit");
}
system("cls");
}while(1);
return 0;
}
struct STU* creatList()
{
struct STU *head,*pcur,*pnew;
head=pcur=(struct STU*)malloc(sizeof(struct STU));
head->next=NULL;
insertStu(head);
printf("链表创建完成\n");
return head;
}
struct STU *getStuList()
{
struct STU *pcur,*head,*pnew;
head=(struct STU *)malloc(sizeof(struct STU));
FILE *fp;
head->next=NULL;
pcur=head;
char fileName[20];
printf("请输入导入文件名:\n");
scanf("%s",fileName);
if((fp=fopen(fileName,"r"))==NULL)
{
printf("打开失败!");
return;
}
fseek(fp,1,1);
while(!feof(fp))
{
fseek(fp,-1,1);
pnew=(struct STU *)malloc(sizeof(struct STU));
pnew->next=NULL;
fread(pnew,sizeof(struct STU),1,fp);
pcur->next=pnew;
pcur=pcur->next;
fgetc(fp);
}
fclose(fp);
printf("读取完成!");
return head;
}
void putStuList(struct STU *head)
{
if(head==NULL)
{
printf("还没有打开或创建列表!\n");
return;
}
struct STU *pcur=head->next;
char fileName[20];
FILE *fp;
printf("请输入导出文件名:\n");
scanf("%s",fileName);
remove(fileName);
if((fp=fopen(fileName,"a"))==NULL)
{
printf("打开失败!");
return;
}
while(pcur!=NULL)
{
fwrite(pcur,sizeof(struct STU),1,fp);
pcur=pcur->next;
}
fclose(fp);
printf("导入完成!");
}
int countStu(struct STU *head)
{
if(head==NULL)
{
printf("还没有打开或创建列表!\n");
return 0;
}
struct STU *pcur;
int count=0;
pcur=head;
while(pcur->next!=NULL)
{
pcur=pcur->next;
count++;
}
return count;
}
void searchStu(struct STU *head,char command)
{
if(head==NULL)
{
printf("还没有打开或创建列表!\n");
return;
}
struct STU *pcur,*ptemp;
int searchInt;
char searchStr[50];
if(command!='A')
{
while(!(command=='1' || command=='2' || command=='3' || command=='4'))
{
printf("┏━━━━━━┓\n");
printf("┃输入查找依据┃\n");
printf("┣━━━━━━┫\n");
printf("┃1.按姓名查找┃\n");
printf("┃2.按ID查找┃\n");
printf("┃3.按年龄查找┃\n");
printf("┃4.按地址查找┃\n");
printf("┃0.返回上一层┃\n");
printf("┗━━━━━━┛\n");
command=getch();
if(command=='0')
{
return;
}
}
printf("输入查找关键字:\n");
if(command=='1' || command=='4')
{
scanf("%s",searchStr);
}
else if(command=='2' || command=='3')
{
scanf("%d",&searchInt);
}
}
pcur=head;
printf("┏━━━━━┳━━━━━┳━━┳━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃%10s┃%10s┃%4s┃%40s┃\n","姓名","学号","年龄","地址");
while(pcur->next!=NULL)
{
ptemp=pcur;
pcur=pcur->next;
if (command=='A'
|| (command=='1' && strcmp(pcur->name,searchStr)==0)
|| (command=='4' && strcmp(pcur->address,searchStr)==0)
|| (command=='2' && pcur->id==searchInt)
|| (command=='3' && pcur->age==searchInt))
{
printf("┣━━━━━╋━━━━━╋━━╋━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃%10s┃%10d┃%4d┃%40s┃\n",pcur->name,pcur->id,pcur->age,pcur->address);
}
}
printf("┗━━━━━┻━━━━━┻━━┻━━━━━━━━━━━━━━━━━━━━┛\n");
}
void insertStu(struct STU *pcur)
{
if(pcur==NULL)
{
printf("还没有打开或创建列表!\n");
return;
}
struct STU *pnew;
int i,total;
char name[10];
printf("请输入插入学生个数:\n");
scanf("%d",&total);
for(i=0;i<total;i++)
{
printf("请输入第%d个学生的信息(姓名、学号、年龄、地址):\n",i+1);
scanf("%s",name);
if(name[0]=='0' && name[1]==0)
{
break;
}
pnew=(struct STU*)malloc(sizeof(struct STU));
pnew->next=pcur->next;
pcur->next=pnew;
pcur=pnew;
strcpy(pcur->name,name);
scanf("%d%d%s",&pcur->id,&pcur->age,pcur->address);
}
}
void deleteStu(struct STU *head)
{
if(head==NULL)
{
printf("还没有打开或创建列表!\n");
return;
}
char c;
struct STU *pcur,*ptemp;
printf("请输入要删除学生的ID(多个用空格隔开):\n");
int delId;
while(scanf("%c",&c),c!='\n')
{
if(c==' ' || c==',')
{
continue;
}
delId=c-'0';
pcur=head;
while(pcur->next!=NULL)
{
ptemp=pcur;
pcur=pcur->next;
if(pcur->id==delId)
{
ptemp->next=pcur->next;
free(pcur);
printf("删除成功 ID:%d!\n",delId);
pcur=ptemp;
}
}
}
}
void tapToContinue(char *tip)
{
if(tip[0]==0)
{
printf("点击任意键继续...\n");
}
else
{
printf("%s",tip);
}
getch();
}
(/code)
张斌老师版本:
(code:C)
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[20];
int num;
int eScore;
int cScore;
int mScore;
struct Student * pNext;
};
int main()
{
int total, index=0;
printf("请输入班级人数:");
scanf("%d", &total);
//定义头结点指针
struct Student *pHeader;
pHeader = (struct Student *) malloc(sizeof(struct Student));
//定义新的数据节点指针
struct Student *pNewNode;
//定义当前节点指针,让其指向头结点
struct Student *pCurNode = pHeader;
printf("请输入每个同学的姓名、学号、英语成绩、数学、C语言:\n");
while(index < total)
{
//为新节点开辟空间,并赋值
pNewNode = (struct Student *) malloc(sizeof(struct Student));
//给pNewNode指向节点赋值
scanf("%s", pNewNode->name);
scanf("%d,%d,%d,%d", &pNewNode->num,&pNewNode->eScore, &pNewNode->mScore, &pNewNode->cScore);
pNewNode->pNext = NULL;
//让当前节点的Next指向新创建的节点
pCurNode->pNext = pNewNode;
//当前节点指针后移,为下一次创建新节点连接到链尾做准备
pCurNode = pNewNode;
index++;
}
printf("输出班级内学生信息:\n");
//让当前节点指针重新指向头结点,从头开始访问链表中所有节点
pCurNode = pHeader;
//还有下一个节点
while(pCurNode->pNext != NULL)
{
pCurNode = pCurNode->pNext;
printf("%s,%d,%d,%d,%d\n", pCurNode->name, pCurNode->num, pCurNode->eScore, pCurNode->mScore, pCurNode->cScore);
}
return 0;
}
(/code)