• 一个奇怪的错误的警示


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

    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
    ", i + 1, node->data);
     52         node = node->next;
     53         i++;
     54     }
     55     printf("disp finished!
    ");
     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
    ", i + 1, node->data);
     67         node = node->next;
     68         i++;
     69     }
     70     printf("disp finished!
    ");
     71
     72     return;
     73 }

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

  • 相关阅读:
    史上最强大vimrc
    Linux 宿主目录、根目录及/home区别
    ubuntu配置软件源
    Lex入门2
    域名服务器(DNS)工作原理
    SQL Server 2005脚本编辑窗口不能使用Enter,Backspace, Insert等按键
    建立windows2003 域名服务器
    JavaScript操作cookie
    VS2008下设置断点调试JavaScript (IE)
    DNS域名服务器原理与架设(Bind on Linux)
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/7719333.html
Copyright © 2020-2023  润新知