几年前就把谭浩强老师的《C语言设计》看完了,但对结构体是一知半解,自认为掌握了,现在学《C算法》时发现才没有直正了解。
# include <stdlib.h>
typedef struct node * link;
struct node
{
int item;
link next;
}
main ()
{
int i,N=9,M=5;
float a;
link t=malloc(sizeof *t),x=t;
t->item=1;
t->next=t;
for (i=2;i<=N;i++)
{
x=(x->next=malloc(sizeof * x));
x->item=i;
x->next=t;
}
while(x!=x->next)
{
for (i=1;i<M;i++)x=x->next;
x->next=x->next->next;
N--;
}
printf ("%d/n",x->item);
}
这是书中的源代码。开始我没有理解 x=(x->next=malloc(sizeof * x));
后来才知道这里的x和x->next指针(地址)。