c语言程序求助(不知错在那?)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
struct student
{
int num;
char name[20];
int age;
char sex[10];
char add[40];
struct student *next;
};
struct student *InHead()//定义头结点
{
struct student *p1;
p1=(struct student*)malloc(sizeof(struct student));
if(p1!=NULL)
{
p1->age=0;
p1->next=NULL;
}
else
printf("定义头结点失败!\n");
return p1;//返回头指针
}
struct student *AddInformation( struct student *head)//增加学生信息
{
struct student *p1,*p2,*p3;
p3=p1=head;
p2=(struct student*)malloc(sizeof(struct student));
if(p2!=NULL)
{
printf("请输入学生学号:\n");
scanf("%d",&p2->num);
printf("请输入学生姓名:\n");
scanf("%s",p2->name);
printf("请输入学生年龄:\n");
scanf("%d",&p2->age);
printf("请输入学生性别:\n");
scanf("%c",p2->sex);
printf("请输入学生地址:\n");
scanf("%s",p2->add);
while(p3->next!=NULL)
{
p3=p3->next;
}
p3->next=p2;
p2->next=NULL;
printf("增加学生信息成功!\n");
system("pause");
system("cls");
}
else
{
printf("分配存储空间失败!\n");
system("pause");
}
return p1; //返回头指针
}
void DisplayInformation(struct student *head)//显示所有学生信息
{
struct student *p1;
p1=head->next;
while(p1!=NULL)
{
printf("学号:%d\n",p1->num);
printf("姓名:%s\n",p1->name);
printf("年龄:%d\n",p1->age);
printf("性别:%c\n",p1->sex);
printf("地址:%s\n",p1->add);
p1=p1->next;
}
system("pause");
system("cls");
}
void DeleteInformation(struct student *p)//删除某个学生信息
{
struct student *p1;
p1=p->next;//删除p1所指向的学生信息
if (NULL!=p)
{
p->next=p->next->next;
free(p1);//释放存储空间,并且将改结点指针指向0
p1=0;
printf("删除学生信息成功!\n");
system("pause");
system("cls");
}
}
void UpdataInformation(struct student *p)//更新该节点的学生信息
{
struct student *p1;
p1=p->next;
if(p!=NULL)
{
p = p->next;
scanf("%d%s%c%d%s",&p->num,p->name,p->sex,&p->age,p->add);
printf("更新学生信息成功!\n");
}
else
printf("更新学生信息失败!\n");
system("pause");
system("cls");
}
void SearchStudentInformation(struct student *p)//查找某个学生的信息
{
struct student *p1;
if (NULL!=p)
{
p1=p->next;//p1指向的该节点为所需要查找的学生信息
printf("所查找的学生信息是:学号 %d 姓名 %s 年龄 %d 性别 %c 地址 %s\n",&p1->num,p1->name,&p1->age,&p1->sex,&p1->add);
}
else
{
printf("该学生不存在!\n");
}
}
void Print_1()
{
printf("************欢饮使用学生信息管理系统**********\n");
printf("***1.插入学生信息 **\n");
printf("***2.显示学生信息 **\n");
printf("***3.删除学生信息 **\n");
printf("***4.查找学生信息 **\n");
printf("***5.更新学生信息 **\n");
printf("***0.退出系统 **\n");
printf("**********************************************\n");
printf("请输入您的操作选择:(0--5)\n");
}
struct student * SearchStudentPoint(struct student *head,char p[])//根据姓名查找某个学生是否存在
{
struct student *p1,*p2;
if (NULL==head->next)
return 0;
else
{
p1=head->next;
p2=head;
while(p2->next!=NULL)
{
if(0==strcmp(p1->name,p))
return p2;//找到该学生并返回该生的结点指针
else
{
p2=p2->next;
p1=p1->next;
}
}
return 0;
}
}
int main(int nArgc,char *szArgv[])
{
char name[20];
char c=1;
struct student *head,*searchp;
head=InHead();//创建学生信息表头结点
while(c!='0')
{
Print_1();
fflush(stdin);
c=getchar();
switch(c)
{
case '1':
AddInformation(head);
break;
case '2':
DisplayInformation(head);
break;
case '3':
printf("请输入所要删除学生的姓名:\n");
scanf("%s",name);
searchp=SearchStudentPoint(head,name);
if (NULL==searchp)
printf("该学生不存在!\n");
else
DeleteInformation(searchp);
break;
case '4':
printf("请输入所要查找学生的姓名:");
scanf("%s",name);
searchp=SearchStudentPoint(head,name);
if (NULL!=searchp)
{
SearchStudentInformation(searchp);
system("pause");
system("cls");
}
else
{
printf("该学生不存在!\n");
system("pause");
system("cls");
}
break;
case '5':
printf("请输入所要更新学生的姓名:\n");
scanf("%s",name);
searchp=SearchStudentPoint(head,name);
if (NULL==searchp)
printf("该学生不存在!\n");
else
UpdataInformation(searchp);
system("pause");
system("cls");
break;
case '0':
return 0;
default:
printf("该选择不存在!\n");
system("pause");
system("cls");
break;
}
}
}