【问题标题】:How do I fix the pop_front and pop_back functions?如何修复 pop_front 和 pop_back 函数?
【发布时间】:2017-12-07 20:16:42
【问题描述】:

我在 C++ 中创建一个双链表,运行代码时出现异常。异常是抛出异常:写访问冲突。 This->head 是 nullptr。

我对这意味着什么感到困惑,并且程序在此错误后崩溃。

这是我的代码:

#include <iostream>
#include "My_list.h"

template<typename T>
My_list<T>::My_list()
{
    head = nullptr;
    tail = nullptr;
    size = 0;
    empty = true;
}

template<typename T>
My_list<T>::~My_list()
{
    while (head)
    {
        My_node<T>* next_node = head->next;
        delete head;
        size--;
        head = next_node;
    }
}

template<typename T>
My_list<T>::My_list(const My_list<T>& copy_list)
{
    size = copy_list.size;
    head = copy_list.head;
    tail = copy_list.tail;
    while (copy_list.head)
    {
        My_node<T>* next = copy_list->head->next;
        head->next = copy_list->next;
        size++;
        head = next;
    }
}

template<typename T>
My_list<T>::My_list(const My_list&& new_list)
{
    //add
}


template<typename T>
void My_list<T>::push_front(T data)
{
    My_node<T>* new_node = new My_node<T>(data);
    new_node->next = nullptr;
    new_node.previous = nullptr;

    if (is_empty())
    {
        head = new_node;
        tail = head;
        size++;
        empty = false;

    }
    else
    {
        head->previous = new_node;
        new_node->next = head;
        head = new_node;
        size++;
        empty = false;
    }
}

template<typename T>
void My_list<T>::push_back(T data)
{
    My_node<T>* new_node = new My_node<T>(data);
    new_node->previous = nullptr;
    new_node->next = nullptr;

    if (is_empty())
    {
        head = new_node;
        tail = head;
        size++;
        empty = false;
    }
    else
    {
        tail->next = new_node;
        new_node->previous = tail;
        tail = new_node;
        size++;
        empty = false;
    }
}
template<typename T>
T My_list<T>::pop_front()
{
    if (!is_empty())
    {
        My_node<T>* temp = head;
        head = head->next;
        head->previous = nullptr;
        size--;
        return temp->get_data();
    }
    else
        cout << "The list is empty and cannot pop anything from it" << endl;
}

template<typename T>
T My_list<T>::pop_back()
{
    if (!is_empty())
    {
        My_node<T>* temp = tail;
        tail = tail->previous;
        tail->next = nullptr;
        size--;
        return temp->get_data();
    }
}

template<typename T>
T My_list<T>::front()
{
    return head->get_data();
}

template<typename T>
T My_list<T>::back()
{
    return tail->get_data();
}


template <typename T>
bool My_list<T>::is_empty()
{
    if (empty)
    {
        return true;
    }
    else
        return false;
}

主文件:

#include <iostream>
#include "My_list.h"
#include "My_node.h"
#include "My_node.cpp"
#include "My_list.cpp"
using namespace std;

int main()
{
    //list<int> list1;

    //list1.push_back(12);

    //cout << list1.front() << endl;
    My_list<int> list;
    list.push_back(22);

    cout << list.front() << endl;

    cout << "BEFORE POP" << endl;
    cout << list.pop_front() << endl;
    cout << "we did it!" << endl;

    cin.get();
    return 0;
}

我意识到我尚未修复的代码可能还有其他问题。我只想让基本的 pop、push 功能正常工作,然后可以解决其他问题。也许问题出在我的其他函数之一上,比如复制构造函数?如果您发现还有其他值得修复的地方,那就太好了!

谢谢。

【问题讨论】:

    标签: c++ visual-studio c++11


    【解决方案1】:

    pop_frontpop_back 使用 is_empty() 但从不更新 empty。因此,一旦添加了一个元素,这些方法就不会认为列表是空的,因此会取消对空指针的引用。

    你需要修改pop_frontpop_back来检查删除项目后列表是否为空,如果是,将empty设置为true

    另一种解决方案是将is_empty() 更改为查看headtail 以确定列表是否为空。这种方式将消除拥有empty 成员的需要。

    【讨论】:

    • 绝对选择“替代解决方案” - 您拥有的冗余状态越少,不同步的状态就越少。
    猜你喜欢
    • 2019-04-23
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2020-02-28
    相关资源
    最近更新 更多