C 列表段错误

标签 c list segmentation-fault

我尝试在 C 中实现链接列表。但是,当我想打印列表时,它会出现段错误。这可能是因为我在 Debug模式下发现列表仍然为空。我哪里搞砸了(ofc AppendLast() 和 AppendFirst() 都不起作用。)?我没有任何编译器错误或警告。这是我的代码:

main.c:

#include "listops.h"

int main(){
    list L, M;
    L = initList(L);
    M = initList(M);

    nodeData myNumbers[5] = {{1},{2},{3},{4},{5}};

    int i;
    ndp myNumbersPtr;

    for(i=0;i<5;i++){
        myNumbersPtr = &myNumbers[i];
        AppendLast(L, myNumbersPtr);
    }

    printList(L);


    return 0;
}

listops.h:

#include <stdlib.h>
#include <stdio.h>

#ifndef LISTOPS_H
#define LISTOPS_H

typedef struct NodeData{
    int myInt;
}nodeData, *ndp;

typedef struct Node{
    ndp data;
    struct Node* next;
} node, *nodeptr;

typedef struct List{
    nodeptr first;
    nodeptr last;
} list, *listptr;

list initList(list L){
    L.first = NULL;
    L.last = NULL;
    return L;
}

int isListEmpty(list L){
    return (L.first == NULL && L.last == NULL);
}

nodeptr createNode(ndp item){
    nodeptr np;
    np = (nodeptr)malloc(sizeof(node));
    np->data = item;
    np->next = NULL;
    return np;
}  

void AppendFirst(list L, ndp item){
    /* node erstellen */
    nodeptr np = createNode(item);
    /* Checken, ob Liste leer */
    if(isListEmpty(L)){
        L.first = np;
        L.last = np;
    }
    else{
        np->next = L.first;
        L.first = np;
    }
}

void AppendLast(list L, ndp item){
    nodeptr np = createNode(item);
    if(isListEmpty(L)){
        L.first = np;
        L.last = np;
    }
    else{
       L.last->next = np;
       np->next = NULL;
    }
}

void printList(list L){
    nodeptr np = L.first;
    int nodeCount = 1;
    while(np->next!=NULL){
        printf("data #%d: %d\n", nodeCount, np->data->myInt);
        nodeCount++;
        np = np->next;
    }
}

#endif // LISTOPS_H

最佳答案

为了使其正常工作,除了现在通过引用调用结构之外,我对 listops.h 进行了两处更改。

首先,我将 AppendLast() 的 else 分支中的 np->next = NULL 替换为 L->最后= np。

其次,我将 while 循环的条件从 np->next!=NULL 更改为 到 np!=NULL。

#include <stdlib.h>
#include <stdio.h>

#ifndef LISTOPS_H
#define LISTOPS_H

typedef struct NodeData{
    int myInt;
}nodeData, *ndp;

typedef struct Node{
    ndp data;
    struct Node* next;
} node, *nodeptr;

typedef struct List{
    nodeptr first;
    nodeptr last;
} list, *listptr;

list initList(list L){
    L.first = NULL;
    L.last = NULL;
    return L;
}

int isListEmpty(listptr L){
    return (L->first == NULL && L->last == NULL);
}

void AppendFirst(listptr L, ndp item){
    /* node erstellen */
    nodeptr np = createNode(item);
    /* Checken, ob Liste leer */
    if(isListEmpty(L)){
        L->first = np;
        L->last = np;
    }
    else{
        np->next = L->first;
        L->first = np;
    }
}

void AppendLast(listptr L, ndp item){
    nodeptr np = createNode(item);
    if(isListEmpty(L)){
        L->first = np;
        L->last = np;
    }
    else{
       L->last->next = np;
       L->last = np;
    }
}

   void printList(list L){
    nodeptr np = L.first;
    int nodeCount = 1;
    while(np!=NULL){
        printf("data #%d: %d\n", nodeCount, np->data->myInt);
        nodeCount++;
        np = np->next;
    }
}

#endif // LISTOPS_H

关于C 列表段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28594300/

相关文章:

c - RNG 字符打印功能 - 每页有多于一行 RNG 字符? : C

python - 合并具有不同长度的掩码数组的二维列表

c++ - 创建 LinkedList 退出并返回代码 -11 (SIGSEGV)

c++ - 在 D 中使用 C++ 类

c++ - OpenCL 多 GPU 积分 - 将全局大小从 32 更改为 64 时出现段错误

c - 用任意数初始化指针

c++ - 为什么 *- 语法/运算符在 C/C++ 中有效?

python - 要在 python 中映射的列表

c - 分配值表而不更改变量名称或编写花哨的代码

c - 如何调试链表程序?