#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NULL 0
struct node
{
char name[10]; /*学生姓名*/
char score[10]; /*学生分数*/
struct node *next;
};
int n=0; /*学生信息表中的记录条数(不包含头节点)*/
int ok=0; /*是否已经建立链表,ok=1表示已建立*/
struct node *head; /*学生信息链表的表头指针*/
int menu_select() /*菜单功能选择函数*/
{
char sn[20]; /*菜单选择输入*/
printf("\n 学生信息管理系统\n");
printf("=================================\n");
printf(" 1、新建学生信息循环链表\n");
printf(" 2、添 加 学 生 信 息\n");
printf(" 3、查 询 学 生 信 息\n");
printf(" 4、删 除 学 生 信 息\n");
printf(" 5、显示学生信息并做统计\n");
printf(" 6、逆置学生信息循环链表\n");
printf(" 0、退 出 管 理 系 统\n");
printf("=================================\n");
printf("请选择0~6:\n");
for(;;)
{
fflush(stdin);
scanf("%s",sn);
if(sn[0]<'0'||sn[0]>'6'||(strlen(sn)!=1))
printf("输入错误,重选0~6\n");
else break;
}
return((int)(sn[0]-48));
}
void CreatList(void) /*循环链表建立函数*/
{
ok=1; /*置链表是否已经建立的标志为1*/
printf(" 新建学生信息循环链表\n\n");
struct node *p,*q;
head=(struct node *)malloc(sizeof(struct node)); /*下面四行用于申请头结点*/
strcpy(head->name,"0");
strcpy(head->score,"0");
head->next=NULL;
char s='y';
while(s=='y'||s=='Y')
{
p=(struct node *)malloc(sizeof(struct node));
printf("请输入学生的相关信息(example: Allen 95):\n");
fflush(stdin);
scanf("%s%s",p->name,p->score);
if((strlen(p->score)>3)||(strlen(p->score)==0)) /*分数输入错误的判断及处理*/
{
printf("ERROR INPUT !\n");
printf("分数应该在0到100之间!\n");
free(p);
continue;
}
if((strlen(p->score)==1)&&((p->score[0]>'9')||(p->score[0]<'0'))) /*分数输入错误的判断及处理*/
{
printf("ERROR INPUT !\n");
printf("分数应该在0到100之间!\n");
free(p);
continue;
}
if((strlen(p->score)==2)&&(((p->score[0]>'9')||(p->score[0]<'0'))||((p->score[1]>'9')||(p->score[1]<'0'))))
/*分数输入错误的判断及处理*/
{
printf("ERROR INPUT !\n");
printf("分数应该在0到100之间!\n");
free(p);
continue;
}
if((strlen(p->score)==3)&&(strcmp(p->score,"100")!=0)) /*分数输入错误的判断及处理*/
{
printf("ERROR INPUT !\n");
printf("分数应该在0到100之间!\n");
free(p);
continue;
}
p->next=NULL;
if(head->next==NULL) head->next=p;
else q->next=p;
q=p;
printf("继续录入(y or n)?");
fflush(stdin);
scanf("%c",&s);
}
q->next=head;
printf("\n学生信息表建立完毕!\n");
getchar(); /*接收上一步产生的回车符*/
getchar(); /*接收一个回车符后将返回主菜单*/
return;
}
void PrintList(void) /*循环链表输出与计数函数*/
{
struct node *p;
if(ok==0)
{
printf("\n\n 请先建立循环链表!\n");
getchar();
getchar();
return;
}
n=0;
p=head;
while((p->next)!=head)
{
n=n+1;
p=p->next;
}
if(n!=0)
printf("此信息表中共有%d名学生的信息!\n\n",n);
else printf("没有学生记录,是空表!\n");
p=head->next;
while(p!=head)
{
printf("%s %s\n",p->name,p->score);
p=p->next;
}
getchar(); /*接收上一步产生的回车符*/
getchar(); /*接收一个回车符后将返回主菜单*/
return;
}
void FindList(void) /*查询函数*/
{
if(ok==0)
{
printf("\n\n 请先建立循环链表!\n");
getchar();
getchar();
return;
}
struct node *p;
char str[15]; char sign='y'; int flag,i,location=0;
printf(" 查 询 学 生 信 息\n\n");
while(sign=='y'||sign=='Y')
{
printf("请选择查询方式:\n1、按姓名查询; 2、按位置查询;\n请选择(1 or 2):");
fflush(stdin);
scanf("%d",&flag);
if(flag==1)
{
printf("请输入姓名:");
fflush(stdin);
scanf("%s",str);
p=head->next;
while((p!=head)&&(strcmp(p->name,str)!=0))
p=p->next;
if(p==head) printf("此学生的信息不存在!\n");
else
{
printf("找到如下相关信息:\n");
printf("%s %s",p->name,p->score);
}
}
if(flag==2)
{
n=0;
p=head;
while((p->next)!=head)
{
n=n+1;
p=p->next;
}
printf("此信息表中目前共有%d名记录\n",n);
printf("请输入查询位置:");
fflush(stdin);
scanf("%d",&location);
p=head->next;
i=1;
while((p!=head)&&(i<location))
{
p=p->next;
i=i+1;
}
if(p==head) printf("此学生的信息不存在!\n");
else
{
printf("找到如下相关信息:\n");
printf("%s %s",p->name,p->score);
}
}
printf("\n重新查询(y or n):");
fflush(stdin);
scanf("%c",&sign);
}
return;
}
void insert(void) /*添加学生信息函数*/
{
if(ok==0)
{
printf("\n\n 请先建立循环链表!\n");
getchar();
getchar();
return;
}
struct node *p,*q,*r;
printf(" 添 加 学 生 信 息\n\n");
int i,flag,location=0;
char str[15]; char s='y';
while(s=='y'||s=='Y')
{
p=(struct node *)malloc(sizeof(struct node));
printf("请输入要添加的学生信息:\n");
fflush(stdin);
scanf("%s%s",p->name,p->score);
printf("请选择添加方式:\n1、按姓名添加; 2、按位置添加;\n请选择(1 or 2):");
scanf("%d",&flag);
if(flag==2)
{
n=0;
r=head;
while((r->next)!=head)
{
n=n+1;
r=r->next;
}
printf("请输入添加位置(共有%d条记录):",n);
fflush(stdin);
scanf("%d",&location);
q=head;
i=1;
while(((q->next)!=head)&&(i<location))
{
q=q->next;
i=i+1;
}
p->next=q->next;
q->next=p;
}
if(flag==1)
{
printf("您打算在谁前面添加:"); /*若元素不在表中,则将节点插到“末尾”*/
fflush(stdin);
scanf("%s",str);
q=head;
while(((q->next)!=head)&&(strcmp((q->next)->name,str)!=0))
q=q->next;
p->next=q->next;
q->next=p;
}
printf("继续添加(y or n)?");
fflush(stdin);
scanf("%c",&s);
}
printf("已完成添加,新的学生信息表如下:\n\n");
PrintList();
return;
}
void InverseList(void) /*逆置函数*/
{
if(ok==0)
{
printf("\n\n 请先建立循环链表!\n");
getchar();
getchar();
return;
}
struct node *p,*q,*r;
printf(" 拟置学生信息循环链表\n\n");
p=head->next;
q=p->next;
p->next=head;
while(q!=head)
{
r=q->next;
q->next=p;
p=q; q=r;
}
head->next=p;
printf("逆置后的信息表如下:\n");
PrintList();
return;
}
void DelList(void) /*删除函数*/
{
if(ok==0)
{
printf("\n\n 请先建立循环链表!\n");
getchar();
getchar();
return;
}
struct node *p,*q;
int flag,i,location;
char str[15]; char s='y';
printf(" 删 除 学 生 信 息\n\n");
while(s=='y'||s=='Y')
{
printf("请选择删除方式:\n1、按姓名删除; 2、按位置删除;\n请选择(1 or 2):");
scanf("%d",&flag);
if(flag==1)
{
printf("请输入你要删除的学生信息:");
fflush(stdin);
scanf("%s",str);
q=head;
while(((q->next)!=head)&&(strcmp((q->next)->name,str)!=0))
q=q->next;
p=q->next;
q->next=p->next;
free(p);
}
if(flag==2)
{
n=0;
p=head;
while((p->next)!=head)
{
n=n+1;
p=p->next;
}
printf("您打算删除第几条记录(共有%d条记录):",n);
fflush(stdin);
scanf("%d",&location);
q=head;
i=1;
while(((q->next)!=head)&&(i<location))
{
q=q->next;
i=i+1;
}
p=q->next;
q->next=p->next;
free(p);
}
printf("是否继续删除(y or n)?");
fflush(stdin);
scanf("%c",&s);
}
printf("处理后的学生信息表如下:\n");
PrintList();
return;
}
void main()
{
while(1)
{
system("cls");
switch(menu_select())
{ case