求改错,程序不报错,但是不能运行,好像成死循环了
#define NULL 0#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct goods{
int num1;
char name[10];
char sex[6];
int grades[5];
struct goods *next;
};
struct goods *creat(int n)
{
struct goods *pb,*pf,*head;
int i,k;
printf("please input the information(学号,名字,性别,成绩)\n");
for(i=0;i<n;i++)
{
pb=(struct goods*)malloc(sizeof(struct goods));
scanf("%d%s%s",&pb->num1,pb->name,pb->sex);
for(k=0;k<5;k++)
scanf("%d",&pb->grades[k]);
if(i==0)
{
head=pb;
pf=pb;
}
else
{
pf->next=pb;
}
pb->next=NULL;
pf=pb;
}
return head;
}
int total(struct goods *head)
{
struct goods *pb;
int i;
for(i=0,pb=head;pb!=NULL;pb=pb->next,i++)
;
return i;
}
struct goods *ordination3(struct goods *head,int n)
{
void out(struct goods *head);
struct goods *pb,*pf,*pf2,*pff,*p,*pc,*pz;
int min,i,k;
for(i=0;i<n-1;i++)
{
pc=head;
for(k=0;k<i;k++)
pc=pc->next;
pff=head;
for(k=0;k<i-1;k++)
pff=pff->next;
pb=(struct goods*)malloc(sizeof(struct goods));
pb=pc;
min=pc->num1;
p=pc;
for(pf=pb;pb!=NULL;pb=pb->next)
{
if(min<pb->num1)
{
pf2=pf;
p=pb;
min=pb->num1;
}
pf=pb;
}
if(pc!=p)
{
if(pc==head)
{
pz=(struct goods*)malloc(sizeof(struct goods));
if(pc->next!=p)
{
pf2->next=pc;
pz->next=p->next;
p->next=pc->next;
pc->next=pz->next;
head=p;
}
else
{
pc->next=p->next;
p->next=pc;
head=p;
}
}
else if(pc!=head)
{
pz=(struct goods*)malloc(sizeof(struct goods));
if(pc->next==p)
{
pff->next=p;
pc->next=p->next;
p->next=pc;
}
else if(pc->next!=p)
{
pff->next=p;
pf2->next=pc;
pz->next=p->next;
p->next=pc->next;
pc->next=pz->next;
}
}
}
}
return head;
}
void find(struct goods *head,int num)
{
struct goods *pb,*p;
int k;
p=head;
for(pb=head;pb!=NULL;pb=pb->next)
{
if(num==pb->num1)
{
p=pb;
}
}
if(num==p->num1)
printf("学号=%d名字=%s性别=%s\n",p->num1,p->name,p->sex);
printf("成绩\n");
for(k=0;k<5;k++)
printf("%d ",p->grades[k]);
}
void modify(struct goods *head,int num1)
{
struct goods *pb;
int k;
for(pb=head;pb!=NULL;pb=pb->next)
{
if(num1==pb->num1)
break;
}
printf("please input your all message in this thing:sequence numbers,name,price,the numbers,brand\n");
scanf("%d%s%s",&pb->num1,pb->name,pb->sex);
for(k=0;k<5;k++)
scanf("%d",&pb->grades[k]);
printf("学号=%d名字=%s性别=%s\n",pb->num1,pb->name,pb->sex);
printf("成绩\n");
for(k=0;k<5;k++)
printf("%d ",pb->grades[k]);
}
struct goods *delect(struct goods *head,int num1)
{
struct goods *pf,*pb,*p,*pf2;
p=head;
pf2=head;
for(pb=head;pb!=NULL;pb=pb->next)
{
if(num1==pb->num1)
{
p=pb;
pf2=pf;
}
pf=pb;
}
if(p==head)
head=head->next;
else
pf2->next=p->next;
return head;
}
struct goods *insert(struct goods *head,struct goods *pi)
{
struct goods *end,*pb,*pf;
for(end=head;end->next!=NULL;end=end->next)
;
if(pi->num1<=head->num1)
{
pi->next=head;
head=pi;
}
else if(pi->num1>head->num1&&pi->num1<=end->num1)
{
for(pb=head;pb->num1<=pi->num1&&pb!=NULL;)
{
pf=pb;
pb=pb->next;
}
pf->next=pi;
pi->next=pb;
}
else
{
end->next=pi;
pi->next=NULL;
}
return head;
}
void out(struct goods *head)
{
struct goods *pb;
int k;
pb=head;
for(;pb!=NULL;pb=pb->next)
{
printf("学号=%d名字=%s性别=%s\n",pb->num1,pb->name,pb->sex);
printf("成绩\n");
for(k=0;k<5;k++)
printf("%d ",pb->grades[k]);
printf("\n");
}
printf("\n");
}
void main()
{
struct goods *head,*pi;
int i,k;
for(;1;)
{
printf("1、存储10个以上学生的基本数据\n");
printf("2、直接输出检验\n");
printf("3、查找指定学生的信息\n");
printf("4、修改指定学生的信息\n");
printf("5.删除指定学生的信息\n");
printf("6、在指定的学生前或后再插入一个物品的信息\n");
printf("输入想要执行的程序\n");
scanf("%d",&i);
switch(i){
case 1:
printf("1、存储10个以上学生的基本数据(包括物品学号,名字,性别,成绩)\n");
printf("请输入你要存储的数据量\n");
scanf("%d",&i);
head=creat(i);break;
case 2:out(head);printf("\n");break;
case 3:
printf("3、查找指定学生的信息\n");
printf("请输入要查找的物品编号\n");
scanf("%d",&k);
find(head,k);break;
case 4:
printf("4、修改指定学生的信息\n");
printf("请输入你想要改变的编号\n");
scanf("%d",&k);
modify(head,k);
out(head);break;
case 5:
printf("5.删除指定学生的信息\n");
printf("请输入你要删除的编号\n");
scanf("%d",&k);
head=delect(head,k);
out(head);break;
case 6:
printf("6、在指定的学生前或后再插入一个物品的信息\n");
printf("将按编号自动插入\n");
head=ordination3(head,total(head));
printf("please input your message(包括物品学号,名字,性别,成绩)\n");
pi=(struct goods *)malloc(sizeof(struct goods));
scanf("%d%s%s",&pi->num1,pi->name,pi->sex);
for(k=0;k<5;k++)
scanf("%d",&pi->grades[k]);
head=insert(head,pi);
out(head);break;
default:printf("error\n");
}
}
}