首页 > 解决方案 > C++ 链表断点错误与删除

问题描述

嘿,我正在尝试制作一个链表,计算从头到尾添加元素所需的时间。然后从头到尾删除then。我有添加和删除的代码,但是当我从列表中删除最后一个元素时,我遇到了一个涉及内存分配的断点错误。我猜我需要在列表为空时添加故障保护,但我不知道该怎么做。

一些建议将不胜感激,因为我对此很陌生。

谢谢!

我的列表类代码:

class list
{
public:
    struct node {
        int data;
        struct node* next;
    } *head, *tail;

    list() :head(NULL), tail(NULL) {}   // constructor  

    ~list() {
        node* current, * temp;
        current = head;
        temp = head;
        while (current != NULL) {
            current = current->next;
            delete temp;
            temp = current;
        }
    }

    // adding to the end of list  
    void addToEnd(int n) {
        node* newNode = new node;
        newNode->data = n;
        newNode->next = NULL;

        if (head == NULL) {
            head = newNode;
            return;
        }
        node* cur = head;
        while (cur) {
            if (cur->next == NULL) {
                cur->next = newNode;
                return;
            }
            cur = cur->next;
        }
    }
    
    //Add to beginning
    void addToBeginning(int n) {
        node* newNode = new node;
        newNode->data = n;
        newNode->next = NULL;

        if (head == NULL) {
            head = newNode;
            return;
        }
        else {
            newNode->next = head;
            head = newNode;
        }
        
    }

    void deleteFromBeginning() {
        if (head != NULL) {
            node* temp = head->next;
            delete head;
            head = temp;
        }
        if (head == NULL) {
            cout << "Nothing to delete" << endl;
            return;
        }
    }

    void deleteFromEnd() {
        node* removeLastNode = head;
            if (head == NULL)
                return;

            if (head->next == NULL) {
                delete head;
                return;
            }

            // Find the second last node 
            node* second_last = head;
            while (second_last->next->next != NULL)
                second_last = second_last->next;

            // Delete last node 
            delete (second_last->next);

            // Change next of second last 
            second_last->next = NULL;

        }

    //Display List
    void displayList() {
        if (head != NULL) {
// This was also triggering an access violation error when I tried to run this after the delete loop
            for (node* temp = head; temp != NULL; temp = temp->next) { 
                cout << temp->data << " ";
            }
            cout << endl;
        }
    }

};

我想将它保存在一个单独的类中,因为它将成为更大程序的一部分,并且我想最小化主源文件上的代码。

这是主要的代码:

int main() {
    int data;
    list myList;

    for (int i = 0; i < 10; i++){
        myList.addToBeginning(i);
    }
    myList.displayList();

    for (int i = 0; i < 5; i++) {
        myList.deleteFromBeginning();
    }
    myList.displayList();

    for (int i = 0; i < 10; i++) {
        myList.addToEnd(i);
    }
    myList.displayList();

    for (int i = 0; i < 15; i++) {
        myList.deleteFromEnd(); // Error comes here
    }
}

标签: c++linked-listsingly-linked-list

解决方案


必须检查列表是否有多个项目,然后启动 while 循环。当我只剩下 1 个节点时,它导致了一个问题。

void deleteFromEnd() {
            node* temp;
            if (head == NULL)
            {
                cout << " There is no item to delete!" << endl;
                return;
            }
            node* start = head;
            if (start->next != NULL)
            {
                while ((start->next)->next != NULL)
                {
                    start = start->next;
                }
                temp = start->next;
                start->next = NULL;
            }
            else
            {
                temp = start;
                head = NULL;
            }
            delete temp;
        }

推荐阅读