免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1640 | 回复: 2
打印 上一主题 下一主题

[C] 求助 段错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 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 }


哎,.........可怜的自己

论坛徽章:
0
3 [报告]
发表于 2011-01-05 21:37 |只看该作者
呵呵,找到问题就好
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP