+ -
当前位置:首页 → 问答吧 → 急求关于链表程序的问题

急求关于链表程序的问题

时间:2010-07-20

来源:互联网

这一段时间在研究链表,写了个单链表翻转的例子,可是只输出了一个值,调试发现,内存地址正确,可就是在next却变为了0x0,不能访问。
贴代码:
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef struct stu{
  4.         int data;
  5.         struct stu *next;
  6. }student;

  7. student *head = NULL;

  8. student *insert(student **p,int data)
  9. {
  10.         student *new = malloc(sizeof(student));
  11.         new->data = data;
  12.         
  13.         new->next = *p;
  14.         *p = new;
  15.         
  16.         return *p;
  17. }
  18. void output(student *p)
  19. {
  20.         int i = 0;
  21.         while(p)
  22.         {
  23.                 printf("%d:%d\n",++i,p->data);
  24.                 p = p->next;
  25.         }
  26. }
  27. student *reverse(student *pre,student *p)                //反转程序,应该没什么问题
  28. {
  29.         if(!p)
  30.                 return NULL;
  31.         if(!p->next)
  32.         {
  33.                 p->next = pre;
  34.                 return p;
  35.         }
  36.         else
  37.         {
  38.                 student *first;
  39.                 first = reverse(pre,p->next);
  40.                 p->next = pre;
  41.                 return first;
  42.         }
  43. }
  44. int main(void)
  45. {
  46.         insert(&head,12);
  47.         insert(&head,28);
  48.         insert(&head,10);
  49.         insert(&head,17);
  50.         
  51.         
  52.         student *p = reverse(NULL,head);                //调试的时候,display p->next 就显示内存无法访问,返回是first地址
  53.         
  54.         output(p);                        //这里只输出了12,应该可以继续向下走,循环输出,可是p->next就不能访问了
  55.         
  56.         return 0;
  57. }
  58.         #include<stdio.h>
  59. #include<stdlib.h>

  60. typedef struct stu{
  61.         int data;
  62.         struct stu *next;
  63. }student;

  64. student *head = NULL;

  65. student *insert(student **p,int data)
  66. {
  67.         student *new = malloc(sizeof(student));
  68.         new->data = data;
  69.         
  70.         new->next = *p;
  71.         *p = new;
  72.         
  73.         return *p;
  74. }
  75. void output(student *p)
  76. {
  77.         int i = 0;
  78.         while(p)
  79.         {
  80.                 printf("%d:%d\n",++i,p->data);
  81.                 p = p->next;
  82.         }
  83. }
  84. student *reverse(student *pre,student *p)                //反转程序,应该没什么问题
  85. {
  86.         if(!p)
  87.                 return NULL;
  88.         if(!p->next)
  89.         {
  90.                 p->next = pre;
  91.                 return p;
  92.         }
  93.         else
  94.         {
  95.                 student *first;
  96.                 first = reverse(pre,p->next);
  97.                 p->next = pre;
  98.                 return first;
  99.         }
  100. }
  101. int main(void)
  102. {
  103.         insert(&head,12);
  104.         insert(&head,28);
  105.         insert(&head,10);
  106.         insert(&head,17);
  107.         
  108.         
  109.         student *p = reverse(NULL,head);                //调试的时候,display p->next 就显示内存无法访问,返回是first地址
  110.         
  111.         output(p);                        //这里只输出了12,应该可以继续向下走,循环输出,可是p->next就不能访问了
  112.         
  113.         return 0;
  114. }
  115.         
复制代码
求各位高手帮忙解答,小弟先谢谢了。

作者: jasfidfjds   发布时间: 2010-07-20

insert()为啥要传个二级指针。小心把header的地址给改变了。
一般的链表没有这么写的吧。
你可以用gdb调适下。

作者: 怪怪虎   发布时间: 2010-07-20

翻转函数有问题,你要返回值设错了。 而且递归实现会变成循环链表 所以要传2级指针。
这里有一个循环实现的翻转。
  1. void reverse(student **h)
  2. {
  3.         if( *h == NULL)
  4.                 return ;
  5.         student *pre, *cur, *next;
  6.        
  7.         pre = *h;
  8.         cur = (*h)->next;
  9.         while(cur)
  10.         {
  11.                 next = cur->next;
  12.                 cur->next = pre;
  13.                 pre = cur;
  14.                 cur = next;
  15.         }
  16.         (*h)->next = NULL;
  17.         *h = pre;
  18. }
复制代码

作者: peijue   发布时间: 2010-07-20