簡體   English   中英

在鏈表C的末尾插入時出現分段錯誤

[英]Segmentation fault when inserting at the end of a linked list C++

好的,所以我試圖通過逐項添加到末尾來創建項目的鏈接列表,並且我也想打印出結果。

我只顯示我的部分代碼(需要處理的部分),因此請忽略此代碼段中我沒有真正使用的所有庫:

#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

struct Item {
    char letter;
    Item *next;
};

class List {
  public:
    List();
    void InsertEnd(char key);
    void Display();
    bool IsEmpty();
    void SizeOf();
  private:
    Item *head;
    Item *tail;
    int size;
};

List::List() {
    head = NULL;
    tail = NULL;
    size = 0;
}

void List::InsertEnd(char key) {
    //new item we're adding to the end
    Item* addOn = new Item();
    addOn->letter = key;
    addOn->next = NULL;

    //temporary item to traverse through list
    Item* temp = head;

    //if list is empty, head and tail both point to it
    if ( IsEmpty() ) {
        head->next = addOn;
        tail->next = addOn;
    } else {
        //once temp = tail
        if (temp->next == NULL) {
            tail->next = temp;
            temp = addOn;
        }
    }

    //update size of list
    SizeOf();

}

void List::Display() {
    cout << "Items:" << endl;
    for (Item* curr = head->next; curr != NULL; curr = curr->next) {
      cout << curr->letter << endl;
    }

cout << size << " items." << endl;

}

bool List::IsEmpty() {
    if (size == 0)
        return true;
    else 
        return false;
}

void List::SizeOf() {
    size++;
}

int main() {
    List* test = new List;

    test->InsertEnd('A');
    test->InsertEnd('B');
    test->InsertEnd('C');

    test->Display();

    return 0;
}

它可以很好地編譯,但是當我運行它時,我唯一得到的就是“分段錯誤”。 ???

如果列表為空,則head->next將為NULL,但是您說head->next = addOn; 不應該是head = addOn;

實際上,這整個代碼塊都是垃圾:

if ( IsEmpty() ) {
    // head and tail are both null so both of these lines
    // invoke undefined behavior (you cant say NULL->field = something)
    head->next = addOn;
    tail->next = addOn;
} else {
    //once temp = tail
    // temp = head. Shouldn't you just be putting "addOn" as the next item
    // in the list after tail? What if temp->next != NULL?
    if (temp->next == NULL) {
        // tail->next = head (because temp == head). That makes no sense.
        tail->next = temp;
        // So some temp variable = addOn? addOn doesn't actually go into the list?
        // Thats a memory leak right there.
        temp = addOn;
    }
}

用偽代碼,您想要的是這樣的:

if (IsEmpty())
{
    head = addOn;
    tail = addOn;
}
else
{
    // special case when 1 item in the list (i.e. head == tail)
    if (head == tail)
    {
        // new item comes after head
        head->next = addOn;
    }
    else
    {
        // new item comes after tail
        tail->next = addOn;
    }
    // tail is now the item just added.
    tail = addOn;
}

我建議您一步一步進入調試器,並在崩潰時准確查看哪個值為空。 調試器將向您顯示代碼每一步的每個值。 我已經進行了30多年的編程工作,每天我都會一步步完成代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM