小编evi*_*vie的帖子

C 中的 int 链表抛出分段错误

插入到我的链接列表中会导致分段错误,我不确定为什么。这是一个 int 链表。它有时有效,有时则无效。我看不出哪里会出错?也许这与我创建新链表的时间有关。由于链接列表插入最后一个方法在文件读取期间起作用,但在我创建新列表时不起作用。

LinkedList* createLinkedList()
{
    LinkedList* list;

    list = (LinkedList*)malloc(sizeof(LinkedList));
    list->head = NULL;
    list->tail = NULL;
    list->length = 0;

    #ifdef DEBUG
    printf("Linked list. Creation of list complete. Checking list length. %d \n", list->length);
    #endif

    return list;
}

void insertLast(LinkedList* list, int entry)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = entry;

    /*If list is empty*/
    if(list->length == 0)
    {
        list->head = newNode;
        list->tail = newNode;
    }
    else
    {
        newNode->prev = list->tail;
        /*if this is the second item in the list*/ …
Run Code Online (Sandbox Code Playgroud)

c struct linked-list doubly-linked-list function-definition

0
推荐指数
1
解决办法
199
查看次数