2013年1月10日
12:26:04完成,历时2天零碎时间,
心情:好久不见,不如不见。
目的:对函数,结构体和链表熟练。
功能:实现学生管理的功能
#include
#include
#include
struct STU
{
char tel[12];
char name[5];
char qq[12];
struct STU * NEXT;
};
struct STU * phead=(struct STU *)malloc(sizeof(struct STU));
void shuru(int);
void shuchu();
void chakan();
void xiugai();
void tianjia();
void sanchu();
void qingkong();
void main()
{
char xuhao;
char n;
phead->NEXT=NULL;
printf("请输入学生人数:");
n=getchar();
n=n-'0';
while(1)
{
printf("\n1、输入学生信息\n");
printf("2、输出学生信息\n");
printf("3、查看学生信息\n");
printf("4、修改学生信息\n");
printf("5、添加学生信息\n");
printf("6、删除学生信息\n");
printf("7、清空信息\n");
printf("0、退出\n");
printf("请输入序号选择:");
xuhao=getchar();
while(xuhao<'0'||xuhao>'7')//避免错误输入。
xuhao=getchar();
xuhao=xuhao-'0';
switch (xuhao)
{
case 1: shuru(n);break;
case 2: shuchu();break;
case 3: chakan();break;
case 4: xiugai();break;
case 5: tianjia();break;
case 6: sanchu();break;
case 7:qingkong();break;
case 0:goto blank;break;
default:printf("输入错误!\n");
}
}
blank:;
free(phead);
}
void shuru(int N)
{
int i;
struct STU *
pnew;
pnew=phead;
pnew->NEXT=NULL;
for(i=0;i
{
STU * plist;
plist=(struct
STU*)malloc(sizeof(struct STU));
plist->NEXT=NULL;
printf("请输入第%d个学生的姓名电话和QQ:\n",i+1);
scanf("%s%s%s",plist->name,plist->tel,plist->qq);
pnew->NEXT=plist;
pnew=plist;
} //定义链表之后,phead不保存值,只保存下一个节点的指针地址,
}
void shuchu()
{
struct STU * p;
int i=0;
p=phead;
if(NULL==p->NEXT)
printf("无数据!\n");
else
{
p=p->NEXT;
while(p!=NULL)
{
printf("第%d名学生信息:姓名:%s 电话:%s
QQ:%s\n",i+1,p->name,p->tel,p->qq);
p=p->NEXT;
i++;
}
}
}
void chakan()
{
char name1[5];
struct STU * p1;
int i=1;
p1=phead;
if(NULL==p1->NEXT)
printf("无数据!\n");
else
{
printf("请输入要查找的姓名:");
scanf("%s",&name1);
while(p1!=NULL)
{
if(strcmp(p1->name,name1)==0)
{
printf("姓名:%s
电话:%s QQ:%s",p1->name,p1->tel,p1->qq);
i=0;
break;
}
p1=p1->NEXT;
}
if(i)
printf("无此人信息!\n");
}
}
void xiugai()
{
char name2[5];
struct STU * p2;
int i=1;
p2=phead;
if(NULL==p2->NEXT)
printf("无数据!\n");
else
{
printf("请输入要修改的姓名:");
scanf("%s",&name2);
while(p2!=NULL)
{
if(strcmp(p2->name,name2)==0)
{
printf("请输入修改后的姓名电话和QQ:\n");
scanf("%s%s%s",p2->name,p2->tel,p2->qq);
i=0;
break;
}
p2=p2->NEXT;
}
if(i)
printf("无此人信息!\n");
}
}
void tianjia()
{
struct STU * p3;
p3=phead;
while(p3->NEXT!=NULL)
p3=p3->NEXT;
struct STU * p_1=(struct STU
*)malloc(sizeof(struct STU));
p3->NEXT=p_1;
printf("请输入要添加的姓名电话和QQ:\n");
scanf("%s%s%s",p_1->name,p_1->tel,p_1->qq);
p_1->NEXT=NULL;
}
void sanchu()
{
char name3[5];
struct STU * p4;
struct STU * p_2;
int i=1;
p4=phead;
if(NULL==p4->NEXT)
printf("无数据!\n");
else
{
printf("请输入要删除的姓名:");
scanf("%s",&name3);
p_2=p4;
while(p4!=NULL)
{
if(strcmp(p4->name,name3)==0)
{
p_2->NEXT=p4->NEXT;
i=0;
free(p4);
printf("删除成功!\n");
break;
}
p_2=p4;
p4=p4->NEXT;
}
if(i)
printf("无此人信息!\n");
}
}
void qingkong()
{
struct STU * p5=phead;
struct STU * p6=phead;
p5=p5->NEXT;
while(p5!=NULL)
{
p6=p5->NEXT;
free(p5);
p5=p6;
}
phead->NEXT=NULL;
printf("清空成功!\n");
}
在VC6.0测试通过,全模块运行正常,
总结:对链表的使用过还不是很熟练,其间出现大大小小的问题。写得快崩溃了,好不容易坚持写完了。
新手学习,欢迎指正。