将项目添加到双向链表的背面时遇到问题
Trouble Adding an item to the back of a doubly linked list
我是双向链表的新手。 我正在处理几段代码:一个函数将项目添加到前面,另一个函数将项目添加到后面,以及分别从前到后和从后到前输出链表的正向和反向显示方法。
我在输出中遇到了一个错误,我试图理解。 似乎我的addFront和显示功能正在工作,但我的addBack可能是我的错误所在。 我在这篇文章的底部发布了我的输出是什么以及它显示的内容。这是我到目前为止的代码。
#include<iostream>
using namespace std;
class doubleLinkedList
{
private:
class node
{
public:
int data;
node* next;
node* prev;
node(int x)
{
data = x;
next = NULL;
prev = NULL;
}
};
public:
node* head;
node* tail;
int count;
doubleLinkedList(); //default constructor
~doubleLinkedList(); //destructor
void displayForward(); //display items from front to back
void displayBackward(); //display items from back to front
void addFront(int); //add item to front of linked list
void addBack(int); //add item to back of linked list
int removeFront(); //remove item from front of linked list
int removeBack(); //remove item from back of linked list
};
//constructor
doubleLinkedList::doubleLinkedList(){
head = tail = NULL;
count = 0;
}
//destructor
doubleLinkedList::~doubleLinkedList(){
node* current = head;
while(current != NULL)
{
node* previous = current;
current = current->next;
delete previous;
}
head = tail = NULL;
count = 0;
}
//display items in linked list from front to back
void doubleLinkedList::displayForward(){
node* pCurrent = head;
while (pCurrent != NULL)
{
cout << pCurrent->data << " ";
pCurrent = pCurrent->next;
}
cout << count;
}
//display items in linked list from back to front
void doubleLinkedList::displayBackward(){
node* pCurrent = tail;
while (pCurrent != NULL)
{
cout <<pCurrent->data << " ";
pCurrent = pCurrent->prev;
}
cout << count;
}
//add item to front of linked list
void doubleLinkedList::addFront(int x){
node* n = new node(x);
n->next = head;
n->prev = NULL;
if (head != NULL)
head->prev = n;
head = n;
count++;
if (tail == NULL)
tail = n;
}
void doubleLinkedList::addBack(int x){
node* n = new node(x);
n->next = NULL;
n->prev = tail;
tail = n;
count++;
}
//Code:////////////////////
int main()
{
doubleLinkedList list;
list.addBack(40);
list.addBack(50);
list.addBack(60);
list.addFront(30);
list.addFront(20);
list.addBack(70);
list.addBack(80);
list.addFront(10);
list.displayForward(); //10 20 30 8 (the 8 value is the count/size i'm keeping track of)
cout << endl;
list.displayBackward(); //80 70 60 50 40 8
cout << endl;
system("pause");
return 0;
}
我的输出应显示 10 20 30 40 50 60 70 80和 80 70 60 50 40 30 20 10
而是我的 displayForward 显示我添加到前面的项目,我的 displayBackward 显示我添加到后面的项目。
你忘了将旧尾巴设置为指向新尾巴的下一个指针。因此,在浏览列表时,最后一个节点的下一个节点仍将指向 NULL。
此外,当添加到背面时,您没有检查头部是否为空,因此在添加到背面时,您只更新了尾部,结果最终有两个单独的列表。
所以你在"end"中添加了 40、50、60,所以尾部指针被设置并相应地更新,但直到你向列表中添加 30 个,继续在前面添加元素,指针相应地更新,但结果头和尾实际上并没有连接。
void doubleLinkedList::addBack(int x){
node* n = new node(x);
if (head == nullptr)
head = n; //this was your main problem!
if (tail != nullptr)
tail->next = n;
n->next = nullptr;
n->prev = tail;
tail = n;
count++;
}
由于您似乎正在编写C++代码,因此我建议您也习惯使用 nullptr 而不是 NULL。
http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer
相关文章:
- 编译包含字符串的代码时遇到问题
- 遇到新行时,有没有办法停止istream_iterator
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 创建结构的数组时遇到分段错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 为什么我遇到分段错误?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 为什么我在尝试模板时遇到视觉工作室C++错误
- 如何从文本文件中读取数值,直到遇到字符类型?
- 从其他类访问类时遇到问题
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 打印问题.我在数组中打印值时遇到一些问题
- 使用静态成员声明类时遇到问题
- 创建 Spdlog 异步文件记录器时遇到困难
- C++ 填充数组时遇到问题
- 将项目添加到双向链表的背面时遇到问题
- 添加到双向链表的背面时遇到问题