一个奇怪的错误的警示

在实现一个单链表时遇到的错误,记录如下,顺便总结下:

list.h代码如下:

 1 #ifndef list_h
 2 #define list_h
 3 #define DataType int
 4
 5 typedef struct _node{
 6     DataType data;
 7     struct _node *next;
 8 } Node;
 9
10 typedef struct _list{
11     Node *head;
12     Node *tail;
13     Node *current;
14 } List;
15
16 void initList(List *);
17 void addHead(List *, DataType iData);
18 void addTail(List *, DataType iData);
19 void dispList(List *);
20
21 #endif

list.c头文件的实现文件代码如下:

  3 #include <string.h>
  4
  5 #include "list.h"
  6
  7 //链表初始化
  8 void initList(List *list){
  9     list->head = NULL;
 10     list->tail = NULL;
 11     list->current = NULL;
 12 }
 13 //链表头插法
 14 void addHead(List *list, DataType iData){
 15     Node *node = (Node *)malloc(sizeof(Node));
 16     node->data = iData;
 17     node->next = NULL;
 18
 19     if(list->head == NULL){
 20         list->tail = node;
 21     }else{
 22         node->next = list->head;
 23     }
 24     list->head = node;
 25
 26     return;
 27 }
 28
 29 //链表尾插法
 30 void addTail(List *list, DataType iData){
 31     Node *node = (Node *)malloc(sizeof(Node));
 32     node->data = iData;
 33     node->next = NULL;
 34
 35     if(list->head == NULL){
 36         list->head = node;
 37     }else{
 38         list->tail->next = node;
 39     }
 40     list->tail = node;
 41
 42     return;
 43
 44 }
 45
 46 //链表输出
 47 void dispList(List *list){
 48     Node *node = list->head;
 49     int i = 0;
 50     while(node != NULL){
 51         printf("the %dth node: %d\n", i + 1, node->data);
 52         node = node->next;
 53         i++;
 54     }
 55     printf("disp finished!\n");
 56
 57     return;
 58 }

测试文件testList.c代码如下:

 1 #include "list.h"
 2 #include <stdlib.h>
 3
 4 int main(int argc, char **argv)
 5 {
 6     List *list1 = (List *)malloc(sizeof(List));
 7     initList(list1);
 8     addHead(list1, 1);
 9     addHead(list1, 3);
10     addHead(list1, 5);
11     addHead(list1, 7);
12     addHead(list1, 9);
13     dispList(list1);
14
15     List *list2 = (List *)malloc(sizeof(List));
16     initList(list2);
17     addTail(list2, 1);
18     addTail(list2, 3);
19     addTail(list2, 5);
20     addTail(list2, 7);
21     addTail(list2, 9);
22     dispList(list2);
23
24     return 0;
25 }

使用命令:gcc  -g  testList.c  list.c  -o testList

./testList执行没有问题。

当list.c代中的dispList()为如下实现时:

 //链表输出
 62 void dispList1(List *list){
 63     Node *node = list->head;
 64     int i = 0;
 65     while(node != NULL){
 66         printf("the %dth node: %d\n", i + 1, node->data);
 67         node = node->next;
 68         i++;
 69     }
 70     printf("disp finished!\n");
 71
 72     return;
 73 }

执行程序时,会出现段错误。

转载于:https://www.cnblogs.com/guochaoxxl/p/7719333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值