链表问题

来源:互联网 发布:学c语言转java容易吗 编辑:程序博客网 时间:2024/04/28 03:19

几年前就把谭浩强老师的《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指针(地址)。

原创粉丝点击