Chinaunix

标题: 求助 段错误 [打印本页]

作者: flycms    时间: 2011-01-05 20:28
标题: 求助 段错误
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<setjmp.h>

  4. typedef struct node * Node;

  5. Node head;
  6. jmp_buf env;

  7. struct node{
  8.         int val;
  9.         Node next;
  10. };

  11. int insert(int val)
  12. {
  13.         Node p,q;
  14.         p= head;

  15.         if(p!=NULL)
  16.         {
  17.         while(p->next !=NULL)
  18.         {p=p->next;}
  19.         }

  20.         q=(Node)malloc(1*sizeof(struct node));
  21.         if(q == NULL) return -1;
  22.         q->next = NULL;
  23.         q->val = val;

  24.         if(p==NULL){
  25.                 head=q;
  26.                 return 1;
  27.         }
  28.         p->next=q;
  29.         return 1;
  30. }

  31. void destroy()
  32. {
  33.         Node p = head;
  34.         while(p!=NULL)
  35.         {
  36.                 Node q;
  37.                 q=p;
  38.                 p=p->next;
  39.                 free(q);
  40.         }

  41.         head = NULL;
  42. }

  43. void print(Node head)
  44. {
  45.         Node p;
  46.         p = head;
  47.         while(p!=NULL)
  48.         {
  49.           printf("%s\n",p->val);
  50.           p = p->next;
  51.         }
  52. }

  53. int main(void){

  54. Node p;
  55. int i;
  56. int res = -1;

  57. res=setjmp(env);
  58. printf("%s\n",res);
  59. if(res !=0 )
  60.         goto err;
  61. printf("insert\n");       
  62. for(i=1;i<8;i++)
  63. insert(i);

  64. print(head);

  65. res = 0;
  66. err:
  67.         destroy();

  68.         return res;
  69. }
复制代码

作者: flycms    时间: 2011-01-05 21:15
上边的print()函数错误
应该是printf("%d\n",p->val);

void print(Node head)
     54 {
     55     Node p;
     56     p = head;
     57     while(p!=NULL)
     58     {
     59       printf("%d\n",p->val);
     60       p = p->next;
     61     }
     62 }


哎,.........可怜的自己
作者: rain_fish    时间: 2011-01-05 21:37
呵呵,找到问题就好




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2