当前位置: 代码迷 >> C语言 >> [求助]想不通
  详细解决方案

[求助]想不通

热度:268   发布时间:2006-04-12 16:53:00.0
[求助]想不通

为什么这两段代码执行的结果不一样?

都输入3 1 7 2 4 8 4 0

但第一个输出6-->1-->4-->7-->2-->3-->5-->NULL

第二个输出1-->4-->7-->2-->3-->5-->NULL

这是为什么呢?

#include<stdio.h>
#include<stdlib.h>
struct circle
{
int data;
int code;
struct circle * ptr;
};
typedef struct circle LIST;
typedef LIST *LISTPTR;
LISTPTR uncode(LISTPTR);
void vprint(LISTPTR);
void creatList(LISTPTR);
main()
{
LISTPTR a;
a=malloc(sizeof(LIST));
a->ptr=NULL;
creatList(a);
vprint(uncode(a));
getch();

  return 0;
}
void creatList (LISTPTR a)
{
LISTPTR b,s;
int c,j=1;
b=malloc(sizeof(LIST));
b->ptr=NULL;
b=a;
scanf("%d",&c);
b->data=c;
b->code=j;
while(1)
{
scanf("%d",&c);
if(c==0) break;
j++;
s=malloc(sizeof(LIST));
s->data=c;
s->code=j;
b->ptr=s;
b=s;
}
b->ptr=a;
}
void vprint(LISTPTR e)
{
while(e!=NULL)
{
printf("%d-->",e->code);
e=e->ptr;
}
printf("NULL\n");
}
LISTPTR uncode(LISTPTR b)
{
int m=20,k=1,j=1;
LISTPTR c,a;
a=b;
b=malloc(sizeof(LIST));
b->ptr=NULL;
c=b;
while(1)
{
while(1)
{
if(k>=m-1) break;
a=a->ptr;
if(a==a->ptr)
{
j=0;
break;
}
k++;
}
m=a->ptr->data;
k=1;
c->ptr=a->ptr;
c=a->ptr;
a->ptr=a->ptr->ptr;
if(m!=1) a=a->ptr;
if(j==0)
{
b=b->ptr;
c->ptr=NULL;
break;
}
}
return b;
}

#include<stdio.h>
#include<stdlib.h>
struct circle
{
int data;
int code;
struct circle * ptr;
};
typedef struct circle LIST;
typedef LIST *LISTPTR;
void uncode(LISTPTR);
void vprint(LISTPTR);
void creatList(LISTPTR);
main()
{
LISTPTR a;
a=malloc(sizeof(LIST));
a->ptr=NULL;
creatList(a);

uncode(a);

vprint(a);
getch();

return 0;
}
void creatList (LISTPTR a)
{
LISTPTR b,s;
int c,j=1;
b=malloc(sizeof(LIST));
b->ptr=NULL;
b=a;
scanf("%d",&c);
b->data=c;
b->code=j;
while(1)
{
scanf("%d",&c);
if(c==0) break;
j++;
s=malloc(sizeof(LIST));
s->data=c;
s->code=j;
b->ptr=s;
b=s;
}
b->ptr=a;
}
void vprint(LISTPTR e)
{
while(e!=NULL)
{
printf("%d-->",e->code);
e=e->ptr;
}
printf("NULL\n");
}
void uncode(LISTPTR b)
{
int m=20,k=1,j=1;
LISTPTR c,a;
a=b;
b=malloc(sizeof(LIST));
b->ptr=NULL;
c=b;
while(1)
{
while(1)
{
if(k>=m-1) break;
a=a->ptr;
if(a==a->ptr)
{
j=0;
break;
}
k++;
}
m=a->ptr->data;
k=1;
c->ptr=a->ptr;
c=a->ptr;
a->ptr=a->ptr->ptr;
if(m!=1) a=a->ptr;
if(j==0)
{
b=b->ptr;
c->ptr=NULL;
break;
}
}
}

搜索更多相关的解决方案: include  

----------------解决方案--------------------------------------------------------
  相关解决方案