如何修复pop_front和pop_back函数
How do I fix the pop_front and pop_back functions?
我正在在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_front
和 pop_back
使用 is_empty()
,但从不更新 empty
。因此,一旦添加了一个元素,这些方法就永远不会认为列表是空的,因此删除了Null指针。
您需要修改pop_front
和pop_back
才能检查列表是否在删除后将为空,如果是这样,请将empty
设置为true
。
替代解决方案是更改is_empty()
以查看head
和tail
,以确定列表是否为空。这样,将消除具有empty
成员的需求。
相关文章:
- 在c++队列中使用pop和visit实现线程安全
- 推导 std::vector::back() 的返回类型
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.push_back(vector.back()+1) 是未定义的行为吗?
- C++ push() 和 pop() 方法使用指针的动态 LinkedList 的问题
- C++ 带模板的链表 - 如何创建 pop()?
- std::stack的奇怪行为,pop()返回相同的值
- 为什么以下 POP 功能无法在主机或设备 (CUDA) 上运行?
- C++ 中类成员的堆栈的 pop() 函数
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- 没有相应 POP 的编译指示包(推送)会导致堆栈粉碎
- std::vector using back(), pop_back(), push_back(), 得到'double free or corruption'错误
- vector.back()和vector.end()有什么区别
- 递归推动POP POSTFIX计算器无法正确执行操作,只需将最后一个数字作为结果
- 关于反序列化程序 pop 函数的模板化专用化的歧义
- 列表大小为 1,但 front() 和 back() 不相等
- E0020 标识符"pop"未定义
- 无效地将 void 表达式与队列和 .pop() 一起使用
- 返回C++中没有 back() 方法的容器的最后一个元素?
- 当您通过Qt小部件打开文件时,是否可以'pop-up'文件?