链表法建立学籍系统.docx
- 文档编号:10193909
- 上传时间:2023-02-09
- 格式:DOCX
- 页数:14
- 大小:16.69KB
链表法建立学籍系统.docx
《链表法建立学籍系统.docx》由会员分享,可在线阅读,更多相关《链表法建立学籍系统.docx(14页珍藏版)》请在冰豆网上搜索。
链表法建立学籍系统
#include
#include
/*谭法建立单链表和输出单链表*/
#include
#include
typedefstructnode
{intnum;
charname[20];
structnode*next;
}STD;
#defineLENsizeof(STD)
intmenu_select();
voidhandle_menu1();
voidgame3();
voidgame4();
voidgame5();
STD*insert(STD*head,STD*stud);
STD*del(STD*head,longnum);
STD*search(STD*head,longnum);
STD*head1,*head2;
intn=0;
voidcreat1(void)/*追加法建表*/
{STD*p1,*p2,*p3;
system("CLS");
head1=NULL;
p1=(STD*)malloc(LEN);
p2=p1;
printf("建表方法1追加法建表!
\n");
printf("输入学号姓名,若输入学号为0则退出建表!
\n");
scanf("%d%s",&p1->num,p1->name);
while(p1->num!
=0)
{n++;
if(n==1)
head1=p1;
else
p2->next=p1;
p2=p1;
p1=(STD*)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
if(p1->num!
=0)
{
p3=head1;
while(p3!
=p2)
{
p3=p3->next;
if((p1->num==p3->num)||((strcmp(p1->name,p3->name)==0)))
{
printf("输入重复,请重新输入:
\n");
free(p1);
p1=(STD*)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
break;
}
}
if((p1->num==p2->num)||((strcmp(p1->name,p2->name)==0)))
{
printf("输入重复,请重新输入:
\n");
free(p1);
p1=(STD*)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
}
else;
}
else;
}
free(p1);p2->next=NULL;
scanf("%*c");
printf("按回车键继续!
\n");getchar();
system("CLS");
return;
}
voidcreat2(void)/*插入法建表*/
{STD*p1;
head2=NULL;
system("CLS");
p1=(STD*)malloc(LEN);
printf("建表方法2插入法建表!
\n");
printf("输入学号姓名(输入学号以0结束)!
\n");
scanf("%d%s",&p1->num,p1->name);
while(p1->num!
=0)
{p1->next=head2;
head2=p1;
p1=(STD*)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
}
scanf("%*c");
printf("按回车键继续!
\n");getchar();
system("CLS");
free(p1);
return;
}
voidprint(STD*head)
{STD*p;
p=head;
while(p!
=NULL)
{printf("学号%3d姓名%s\n",p->num,p->name);p=p->next;}
printf("链表已空!
\n");
scanf("%*c");
printf("按回车键继续!
\n");getchar();
system("CLS");
}
/*voidmain()
{
printf("***运行结果***\n");
printf("输入学号姓名(输入学号以0结束)!
\n");
creat1();
print(head1);
creat2();
print(head2);
}*/
/*******************************/
/*主程序文件*/
/*******************************/
voidmain()
{
handle_menu1();
}
/*****************************/
/*菜单处理函数*/
/*****************************/
voidhandle_menu1()
{
inth;
for(;;)
{
h=menu_select();
switch(h)
{case1:
/*printf("\t进入追加法建表\n");*/
creat1();/*调用追加法建表功能模块*/
break;
case2:
/*printf("\t进入插入法建表\n");*/
creat2();/*调用插入法建表功能模块*/
break;
case3:
/*printf("\t输出链表1的结果\n");*/
print(head1);
break;
case4:
/*printf("\t输出链表2的结果\n");*/
print(head2);
break;
case5:
game3();/*调用删除模块*/
break;
case6:
game4();/*调用插入模块*/
break;
case7:
game5();/*调用查找模块*/
break;
case8:
printf("\t再见!
\n");
return;
}
}
}
//**********************************
//*菜单选择函数:
intmenu_select()*
//**********************************
intmenu_select()
{
intcn;
for(;;)
{printf("\n");
printf("\n");
printf("\n");
printf("\t1.追加法建表\n");
printf("\t2.插入法建表\n");
printf("\t3.输出链表1\n");
printf("\t4.输出链表2\n");
printf("\t5.删除一个结点\n");
printf("\t6.插入一个结点\n");
printf("\t7.查找一个结点\n");
printf("\t8.退出程序\n");
printf("\t选择1-8:
");
scanf("%d",&cn);//gets(s);
//cn=atoi(s);
if(cn<1||cn>8)
printf("\n\t输入错误,重选1-8:
");
else
break;
}
returncn;
}
/**********************************/
/*对链表插入操作*/
/*函数原型:
voidgame4();*/
/*函数参数:
void*/
/*函数返回值:
void*/
/**********************************/
voidgame4()
{
charch[5];
STD*stud,*head=head1;system("CLS");
printf("\t仅限在表1中插入!
\n");
printf("\t输入要插入结点的学号和姓名(以空格隔开):
");
stud=(STD*)malloc(LEN);
scanf("%*c");
scanf("%d%s",&stud->num,&stud->name);
if(stud->num<=0)
{printf("\t输入的学号不能为非正数,请重新选择插入!
\n");
return;
}
while(stud->num>0)
{
head1=insert(head,stud);
printf("\tWouldyouwantreinsert?
\n");
printf("\tYesorNo?
\n\t");
scanf("%s",&ch);
if(strcmp(ch,"Yes")==0)
{
printf("\t输入要插入结点的学号和姓名(以空格隔开):
");
head=head1;
stud=(STD*)malloc(LEN);
scanf("%*c");
scanf("%d%s",&stud->num,&stud->name);
}
else
break;
}
printf("\t输入回车键返回!
\n");
getchar();
system("CLS");
printf("\n");
}
/**************************************/
/*插入模块*/
/**************************************/
STD*insert(STD*head,STD*stud)
{
STD*p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!
=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}
/**********************************/
/*对链表删除操作*/
/*函数原型:
voidgame3();*/
/*函数参数:
void*/
/*函数返回值:
void*/
/**********************************/
voidgame3()
{
charch[5];
longnum;
STD*head=head1;system("CLS");
printf("\t仅限在表1中删除!
\n");
printf("\t输入要删除结点的学号:
");
scanf("%*c");
scanf("%ld",&num);
if(num<=0)
{printf("\t输入的学号不能为非正数,请重新选择删除!
\n");
return;
}
while(num>0)
{
head1=del(head,num);
printf("\tYesorNo?
\n");
printf("\tWouldyouwantredelete?
\n");
printf("\tYesorNo?
\n\t");
scanf("%s",&ch);
if(strcmp(ch,"Yes")==0)
{
printf("\t输入要删除结点的学号:
");
scanf("%*c");
head=head1;
scanf("%ld",&num);
}
else
break;
}
printf("\t输入回车键返回!
\n");
getchar();
system("CLS");
printf("\n");
}
/**************************************/
/*删除模块*/
/**************************************/
STD*del(STD*head,longnum)/*删除结点*/
{
STD*p1,*p2;
if(head==NULL)
{
printf("\nlistnull!
\n");
returnhead;
}
p1=head;
while(num!
=p1->num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:
%ld\n",num);
n=n-1;
}
else
printf("%ldnotfound!
\n",num);
return(head);
}
/**********************************/
/*对链表查找操作*/
/*函数原型:
voidgame5();*/
/*函数参数:
void*/
/*函数返回值:
void*/
/**********************************/
voidgame5()
{
charch[5];
longnum;
inta;
STD*head;system("CLS");
printf("\t请选择所要查找的列表(1或者2):
");
scanf("%d",&a);
if(a==1)
head=head1;
else
head=head2;
printf("\t输入要查找结点的学号:
");
scanf("%*c");
scanf("%ld",&num);
if(num<=0)
{printf("\t输入的学号不能为非正数,请重新选择查找!
\n");
return;
}
while(num>0)
{
head1=search(head,num);
printf("\tWouldyouwantredelete?
\n");
printf("\tYesorNo?
\n\t");
scanf("%s",&ch);
if(strcmp(ch,"Yes")==0)
{
printf("\t输入要查找结点的学号:
");
scanf("%*c");
scanf("%ld",&num);
}
else
break;
}
printf("\t输入回车键返回!
\n");
getchar();
system("CLS");
printf("\n");
}
/**************************************/
/*查找模块*/
/**************************************/
STD*search(STD*head,longnum)/*查找结点*/
{
STD*p;
if(head==NULL)
{
printf("\nlistnull!
\n");
returnhead;
}
p=head;
while(num!
=p->num&&p->next!
=NULL)
{
p=p->next;
}
if(num==p->num)
{
printf("查找的学生信息:
学号%3d姓名%s\n",p->num,p->name);
}
else
printf("%ldnotfound!
\n",num);
return(head);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 链表法 建立 学籍 系统