- 论坛徽章:
- 0
|
输入3 1 7 2 4 8 4 0
如果不按注释改的话.输出6-->1-->4-->7-->2-->3-->5-->NULL
按注释改的话.输出1-->4-->7-->2-->3-->5-->NULL
改了之后的uncode函数和没改的函数,对指针b的操作没什么差别.那么结果也应该是一样的.但不一样.这是为什么?
#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);//把这的LISTPTR 改为void
void vprint(LISTPTR);
void creatList(LISTPTR);
main()
{
LISTPTR a;
a=malloc(sizeof(LIST));
a->ptr=NULL;
creatList(a);
vprint(uncode(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");
}
LISTPTR uncode(LISTPTR b))//把这的LISTPTR 改为void
{
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;//这句删掉.
}
[ 本帖最后由 lmatt 于 2006-4-13 20:18 编辑 ] |
|