|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
运行时突然出现debug,请问代码这样打哪里有问题?
void shifang(struct nb *head)
{
struct nb *p1,*p2;
p1=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
head->next=NULL;
printf("OK!");
}
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- #define LEN sizeof(struct nb)
- struct nb
- {
- long xh;
- float cj;
- struct nb *next;
- };
- void sc(struct nb *head)
- {
- struct nb *b;
- b=head;
- if(head)
- {
- do
- {
- printf("%d %f\n",b->xh,b->cj);
- b=b->next;
- }while(b);
- }
- }
- struct nb *xin()
- {
- int n=0;
- struct nb *head,*p1,*p2;
- p1=p2=(struct nb *)malloc(LEN);
- head=NULL;
- scanf("%ld,%f",&p1->xh,&p1->cj);
- while(p1->xh)
- {
- n=n+1;
- if(n==1)
- head=p1;
- else
- p2->next=p1;
- p2=p1;
- p1=(struct nb *)malloc(LEN);
- scanf("%ld,%f",&p1->xh,&p1->cj);
- }
- p2->next=NULL;
- return head;
- }
- struct nb *xj(struct nb *head,struct nb *stud)
- {
- struct nb *p0,*p1,*p2;
- p1=head,p0=stud;
- if(p1==NULL)
- {
- head=p0;
- p0->next=NULL;
- }
- else
- {
- while((p0->xh>p1->xh)&&(p1->next!=NULL))
- {
- p2=p1;
- p1=p1->next;
- }
- if(p0->xh<=p1->xh)
- {
- if(p1==head)
- head=p0;
- else
- p2->next=p0;
- p0->next=p1;
- }
- else
- {
- p1->next=p0;
- p0->next=NULL;
- }
- }
- return head;
- }
- struct nb *del(struct nb *head,int num)
- {
- struct nb *p1,*p2;
- if(head==NULL)
- {
- printf("null");
- goto END;
- }
- p1=head;
- while(num!=p1->xh&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(p1->xh==num)
- {
- if(p1==head)
- head=p1->next;
- else
- p2->next=p1->next;
- }
- else
- printf("not found\n");
- END:
- return head;
- }
- void search(struct nb *head,long number)
- {
- struct nb *p1,*p2;
- p1=head;
- if(head==NULL)
- {
- printf("null\n");
- goto ENG;
- }
- else
- {
- while((p1->xh!=number)&&(p1->next!=NULL))
- {
- p2=p1;
- p1=p1->next;
- }
- if(p1->xh==number)
- printf("%d %f\n",p1->xh,p1->cj);
- else
- printf("not found\n");
- }
- ENG:
- printf("ok!\n");
- }
- void shifang(struct nb *head)
- {
- struct nb *p1,*p2;
- p1=head;
- while(p1)
- {
- p2=p1->next;
- free(p1);
- p1=NULL;
- p1=p2;
- }
- printf("OK!");
- }
- void main()
- {
- int e,f;
- struct nb *a,*b,*c,stu;
- a=xin();
- sc(a);
- printf("\n");
- scanf("%d",&e);
- b=del(a,e);
- sc(b);
- printf("\n");
- scanf("%d,%f",&stu.xh,&stu.cj);
- c=xj(b,&stu);
- sc(c);
- printf("\n");
- scanf("%d",&f);
- search(c,f);
- shifang(c);
- }
复制代码
没发现有错误,只是有些输出因为没加\n,导致前一个数与后一个数连在一起了,我已经加了,还有的是,你的程序最大的问题是,运行到输入的时候竟然没任何提示要输入什么,至少输出点什么提示一下(这个请自己加),还有,你的scanf输入格式要对齐:例如这个scanf("%d,%f",&p1->xh,&p1->cj); 数之间的,千万别忘记写上去,要的是英文格式下输入的逗号,不是中文格式的。
还有,下次发代码请带上注释,都不知道你那函数干嘛用的。
|
|