正文

单链表逆置2006-03-27 11:16:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ljqy/11456.html

分享到:

单链表的逆置的实现:   (1)算法struct link{  int data;  struct link *next;};link reverse(link x){  if( NULL==x )    return NULL;    link t=NULL;  link r=NULL, y=x;  //(0)  while(y!=NULL)  {    t = y->next;   //(1)    y->next = r;   //(2)    r = y;         //(3)     y = t;         //(4)   }  return r;     //返回逆置后的链表}(二)原理(0)(1)(2)(3)(4)分别对应以上程序的各序号第一次while循环-------------------------------------------(0) r=NULL, y=xr=NULL    a    --->    b     --->   c   --->  d --> NULL          |(0)                      y            -------------------------------------------(1) t =y->nextr=NULL    a    --->    b     --->   c   --->  d --> NULL          |(0)         | (1)            y            t --------------------------------------------(2) y->next = r              a    --->  NULL            b     --->   c   --->  d --> NULL|(0)   (2)  |              | (1)  y           r              t  ---------------------------------------------       (3) r = y              a    --->  NULL            b     --->   c   --->  d --> NULL|(0)   (2)                 | (1)  y                          t |(3)r--------------------------------------------- (4) y = t              a    --->  NULL            b     --->   c   --->  d --> NULL|(0)   (2)                | (1)  |                         t |(3)                      | (4)r                         y第二次while循环(并对上面进行整理)--------------------------------------------- (1) t = y->next              a    --->  NULL            b     --->   c   --->  d --> NULL|                          |            |(1)r                          y            t                         --------------------------------------------- (2) y->next = r              b  --->  a    --->  NULL         c   --->  d --> NULL|  (2)   |                       |(1)y        r                       t--------------------------------------------- (3) r = y              b  --->  a    --->  NULL         c   --->  d --> NULL|  (2)                           |(1)y                                t|  (3)r--------------------------------------------- (4) y = t              b  --->  a    --->  NULL         c   --->  d --> NULL|  (2)                           |(1)|                                t|  (3)                           |(4)r                                y第三个循环 (并对上面的进行整理)--------------------------------------------- (1) t = y->next              b  --->  a    --->  NULL         c   --->  d --> NULL|                                |         |(1)r                                y         t以后的与第二次循环同, 最终可得:              d ---> c  ---> b  --->  a    --->  NULL                                    

阅读(14821) | 评论(9)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册