缺少break?
#define Null 0
#define INF sizeof(struct information)
#include<stdlib.h>
#include<stdio.h>
struct information
{
int num;
int amo;
struct information *next;
};
struct information *creat(int n)
{
struct information *head=NULL,*p1,*p2;
head=p2=(struct information *)malloc(INF);
{
p1=(struct information*)malloc(INF);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return(head);
}
struct information *solve(struct information*head,struct information *info)
{
struct information *p3,*p4,*p5;
p3=head;
p5=info;
if(head==NULL)
{
head=p5;
p5->next=NULL;
}
else
{
while((p5->num>p3->num)&&(p3->next!=NULL))
{
p4=p3;p3=p3->next;
}
if (p5->num<=p3->num)
{
if(p5->num==p3->num)
p5->num=(p5->num+p5->num);
else p4->next=p5;
p5->next=p4;
}
else
{
p4->next=p5;p5->next=NULL;
}
}
return(head);
}
void main()
{
int a;
char answer,Y,N;
struct information *p;
struct information *p3;
information *creat();
printf("1->输入内容\n2->查看数据\n请选择\n");
scanf("%d",&a);
if(a==1)
{
printf ("Input the number and amount\n");
scanf ("%d%d",&p3->num,&p3->amo);
printf("Continue? Y/N?\n");
scanf("%c",&answer);
while (answer==Y)
printf("%d,%d\n",&p->num,&p->amo);
p=p->next;
}
else
{
do
{
printf("%d %d\n",p->num,p->amo);
p=p->next;
}
while
(p!=NULL);
}
}
这是最近写的一个程序,可是却一执行的时候就无法结束,难道是缺少break语句吗?大家可以自己执行一下试试
[解决办法]
#include<stdlib.h>#include<stdio.h>struct information{int num;int amo;struct information *next;};#define Null 0#define INF sizeof(struct information)struct information *creat(int n){ struct information *head=NULL,*p1,*p2; head=p2=(struct information *)malloc(INF);// for(int i=0; i < n; ++i)//创建n个节点 { //你这里是什么情况,虽然没有错误,但是不对! //应该是在这里给节点输入数据,只是在这里分配内存 //... p1=(struct information*)malloc(INF); p2->next=p1; p2=p1; } p2->next=NULL; return(head);}struct information *solve(struct information*head,struct information *info){ struct information *p3,*p4,*p5; p3=head; p5=info; if(head==NULL) { head=p5; p5->next=NULL; } else { while((p5->num>p3->num)&&(p3->next!=NULL)) { p4=p3;p3=p3->next; } if (p5->num<=p3->num) { if(p5->num==p3->num) p5->num=(p5->num+p5->num); else p4->next=p5; p5->next=p4; } else { p4->next=p5;p5->next=NULL; } } return(head);}void main(){ int a; char answer,Y,N; struct information *p; struct information p3; //问题一:information *creat()这是什么表示方法? //问题二:你这里要修改一个能够接受返回值head对象作为头结点 //问题三:还有creat()的参数你也该给一个!! printf("1->输入内容\n2->查看数据\n请选择\n"); scanf("%d",&a); if(a==1) { printf ("Input the number and amount\n"); scanf ("%d%d",&p3.num,&p3.amo);// printf("Continue? Y/N?\n"); scanf("%c",&answer); while (answer == 'Y')//Y是什么,你给值吧,不要给个变量,我就给你修改为'Y' printf("%d,%d\n",&p->num,&p->amo); p=p->next; } else { do { printf("%d %d\n",p->num,p->amo); p=p->next; }while(p!=NULL); }}