创建双链表
creating doubly linked list
我只想创建双链接列表并检查它是否为空。请把错误告诉我。显示的错误是:在函数empty()中,头和尾超出了范围。在Dict.类中定义为结构时不起作用
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class node
{ public:
string data;
node* next;
node* prev;
friend class Dict;
};
class Dict
{ public:
bool empty();
Dict();
node* head;
node* tail;
};
Dict::Dict()
{ head=new node;
tail= new node;
head->next=tail;
tail->prev=head;
}
bool empty()
{
return head->next==tail;
}
int main()
{
Dict my_list;
if(my_list.empty())
{cout<<"empty list"<<endl;}
else
{cout<<"Not empty"<<endl;}
}
我认为您只需要在类中包含空方法:
bool Dict::empty()
{
return head->next==tail;
}
首先,您不会创建一个空列表,因为您的构造函数会创建新的节点。
在"empty"方法中,您试图引用Dict类中定义的"head"变量
可能的解决方案:
Dict::Dict()
{
head = tail = null;
}
bool Dict::empty()
{
return head == null;
}
- 有几件事你需要做。首先你没有包括你的node.cpp,只有node.h,你的节点构造函数做什么?在Dict()构造函数上,您应该调用节点构造函数,即node(),这将把节点类初始化为您的节点构造函数所做的任何事情,即将变量字符串数据设置为某个输入值
- empty()的输出是什么。根据你的定义。empty()方法,正在做同样的事情,检查head->next是否与tail位于同一内存位置。我不认为
t think that
是您想要的,因为如果您调用dict()构造函数,它将始终返回true。如果你不调用dict(),它将返回false,或者你甚至可能得到一个句柄错误,"null引用" - 要创建节点列表或链表,您需要定义ADD方法,该方法应该能够添加新节点或将当前节点与下一个节点链接
- 您还需要定义删除节点,用于删除或删除任何给定的节点。这可能是迄今为止你写过的最难的方法
相关文章:
- 使用std::list创建循环链表
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 在C++中为链表类创建实例
- C++ 双向链表:使用矩阵类成员创建和填充列表
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 创建了一个链表,但如何删除 c++ 中的"所有"节点
- 在解决链表问题时创建一个额外的节点是一个好习惯吗?
- 无法创建或返回反向链表
- 在 C++ 中创建单个链表
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 程序在为链表创建推送函数时崩溃
- 自定义链表创建 RtlValidateHeap 错误,其结构具有链表
- 为数据缺少默认构造函数的链表创建节点
- 如何为链表C++创建"弹出尾巴"函数
- 如何为双链表创建交换函数
- 在链表创建循环中插入
- 正在为链表c++创建DataType
- 为链表C++创建节点
- 如何使用swap函数为双重链表创建冒泡排序
- 如何为链表创建复制构造函数