如何修复pop_front和pop_back函数

How do I fix the pop_front and pop_back functions?

本文关键字:pop back 函数 front 何修复      更新时间:2023-10-16

我正在在C 中进行双链接列表,并且在运行代码时会得到例外。例外是抛出的例外:写访问违规。这个 ->头为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,推动功能正常工作,然后可以解决其他问题。也许问题是我的其他功能之一,例如复制构造函数?如果您注意到其他值得修复的事情,那就太好了!

谢谢。

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

您需要修改pop_frontpop_back才能检查列表是否在删除后将为空,如果是这样,请将empty设置为true

替代解决方案是更改is_empty()以查看headtail,以确定列表是否为空。这样,将消除具有empty成员的需求。