拜托 ,帮我看下这程序,达不到预期的结果,我也不知道啥原因
#include<stdio.h>#include<stdlib.h>
#include<string.h>
typedef struct student
{int Num,Number;
char Name[20];
char Sex[3];
struct student *next;}stud;
int menu_select();
stud *create();
void display(stud*head);
stud*insert_a_record(stud*head);
stud*Delete_a_record(stud*head);
void find_a_record(stud*head);
void quit(stud*head);
main()
{stud*head;
for(;;)
{
switch(menu_select())
{case 1:printf("1.建立链表\n");
create();system("pause");
break;
case 2: printf("2.数据浏览\n");
display(head);system("pause");
break;
case 3:printf("3.插入新生\n");
insert_a_record(head);
system("pause");
break;
case 4:printf("4.删除学生\n");
Delete_a_record(head);
system("pause");
break;
case 5:printf("5.查找学生\n");
find_a_record(head);
system("pause");
break;
case 0:printf("0.退出程序\n");
quit(head);
system("pause");
exit(0);}}}
int menu_select()
{char c;
do
{system("cls");
printf("1.建立链表\n");
printf("2.数据浏览\n");
printf("3.插入新生\n");
printf("4.删除学生\n");
printf("5.查找学生\n");
printf("0.退出程序\n");
printf("选择操作(0-5):");
c=getchar();}
while(c>'5'||c<'0');
return(c-'0');}
stud*insert(stud*head,stud*s)
{stud*p;
p=head;
while(p->next!=NULL&&p->next->Num!=s->Num)
p=p->next;
s->next=p->next;
p->next=s;
return head;}
stud*create()
{stud*p,*head;
char name[20];
char sex[3];
int num,number,a;
head=(stud*)malloc(sizeof(stud));
if(head==NULL)
{printf("申请失败\n");
exit(1);}
head->next=NULL;
printf("输入学号,姓名,性别,电话:\n");
scanf("%d",&num);
scanf("%s",&name);
scanf("%c",&sex);
scanf("%d",&number);
while(1)
{p=(stud*)malloc(sizeof(stud));
if(p==NULL)
{printf("申请失败\n");
exit(2);}
p->Num=num;
strcpy(p->Name,name);
strcpy(p->Sex,sex);
p->Number=number;
head=insert(head,p);
system("pause");
printf("再输入学生请按1,否则按0\n");
scanf("%d",&a);
if(a==1)
{printf("输入学号,姓名,性别,电话:\n");
scanf("%d",&num);
scanf("%s",&name);
scanf("%c",&sex);
scanf("%d",&number);}
else break;}
return head;}
void display(stud*head)
{stud*p;
p=head->next;
if(p!=NULL)
{while(p!=NULL)
{printf("%d",p->Num);
printf("%s",p->Name);
printf("%c",p->Sex);
printf("%d",p->Number);
p=p->next;}}}
stud*insert_a_record(stud*head)
{stud*p;
char name[20];
int num,number;
char sex[3];
printf("输入学号,姓名,性别,电话:\n");
scanf("%d",&num);
scanf("%s",&name);
scanf("%c",&sex);
scanf("%d",&number);
p=(stud*)malloc(sizeof(stud));
p->Num=num;
strcpy(p->Name,name);
strcpy(p->Sex,sex);
p->Number=number;
head=insert(head,p);
printf("插入成功\n");
return head;}
stud*Delete(stud *head,char*name)
{stud*p,*w;
p=head;
while(p->next!=NULL&&strcmp(p->next->Name,name)!=0)
p=p->next;
w=p->next;
if(p->next!=NULL)
{p->next=w->next;
free(w);
printf("删除成功\n");}
else printf("插入的位置错误\n");
return head;}
stud*Delete_a_record(stud*head)
{char name[20];
int k;
printf("输入待删学生姓名\n");
scanf("%s",&name);
printf("确认删除按1");
scanf("%d",&k);
if(k==1)
head=Delete(head,name);
return head;}
stud*find(stud*head,char *name)
{stud*p;
p=head->next;
while(p!=NULL)
{if(strcmp(p->Name,name)==0)
return head;
p=p->next;}
return NULL;}
void find_a_record(stud*head)
{stud*p;
char name[20];
char sex[3];
int num,number;
printf("输入查找的姓名:");
scanf("%s",name);
p=find(head,name);
if(p!=NULL)
{printf("查找成功\n");
printf("%d",p->Num);
printf("%s",p->Name);
printf("%c",p->Sex);
printf("%d",p->Number);}
else printf("查无此人");}
void quit(stud*head)
{stud *p;
while(head!=NULL)
{p=head;
head=head->next;
free(p);}
exit(0);}