从双链表类打印单个节点
Printing a Single Node from a Doubly Linked List Class
我试图实现三个方法目前get_first(), get_last()和print_node()。Get_first()将返回列表的头部,get_last()返回尾部,而print_node()将只打印发送给它的节点的数据字段。我试图实现,但不断得到指针错误,我所做的任何改变。
这是我的node.h header:
class Node
{
private:
int data;
Node *next;
Node *prev;
friend class LinkedList;
};
class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList();
~LinkedList();
bool empty();
void insert_left(int v);
void insert_right(int v);
Node* get_first();
Node* get_last();
void print_list();
void print_node(Node *n);
void remove_left();
void remove_right();
protected:
void add(Node *v, int d);
void remove(Node *v);
};
下面是我的list.cpp类实现文件的相关部分:
#include <iostream>
#include "node.h"
using namespace std;
LinkedList :: LinkedList()
{
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
LinkedList :: ~LinkedList()
{
while(!empty())
{
remove_left();
}
delete head;
delete tail;
}
void LinkedList :: add(Node *v, int d)
{
Node *u = new Node;
u->data = d;
u->next = v;
u->prev = v->prev;
v->prev->next = v->prev = u;
}
void LinkedList :: print_list()
{
Node *tmp = head;
while(tmp != NULL)
{
cout << tmp->data << endl;
tmp = tmp->next;
}
}
void LinkedList :: print_node(Node *n)
{
Node *tmp = n;
cout << tmp->data << endl;
}
Node LinkedList :: get_first()
{
return head;
}
Node LinkedList :: get_last()
{
return tail;
}
最后这里是main函数在一个名为main.cpp的文件中:
#include <cstdlib>
#include <iostream>
#include "list.cpp"
using namespace std;
int main(int agrc, char **argv)
{
LinkedList *l = new LinkedList();
//LinkedList *m = new LinkedList();
l->insert_left(200);
l->insert_left(700);
l->insert_left(300);
Node *temp = l->get_first();
//l->print_list();
l->print_node(temp);
delete l;
return 0;
}
下面是当前错误输出:
g++ main.cpp -o main
In file included from main.cpp:3:
list.cpp:85: error: prototype for ‘Node LinkedList::get_first()’ does not match any in class ‘LinkedList’
node.h:24: error: candidate is: Node* LinkedList::get_first()
list.cpp:90: error: prototype for ‘Node LinkedList::get_last()’ does not match any in class ‘LinkedList’
node.h:25: error: candidate is: Node* LinkedList::get_last()
make: *** [main] Error 1
我不确定要做的确切改变,但我认为这与我如何在get_first()和last()函数中返回头部有关。请原谅文章的长度。
您在函数声明中返回Node*,但在定义中您将Node作为返回类型。使用
Node* LinkedList :: get_first()
{
return head;
}
Node* LinkedList :: get_last()
{
return tail;
}
数据成员head
和tail
定义为
Node *head;
Node *tail;
也就是说它们是指向Node的指针。因此,如果任何函数返回head
或tail
,那么它的返回类型必须是Node *
所以这些成员函数定义
Node LinkedList :: get_first()
{
return head;
}
Node LinkedList :: get_last()
{
return tail;
}
是错误的。它们返回头和尾,但没有返回类型Node *
,并且它们的定义与类中的理论声明不一致。
构造函数定义也是错误的。它应该看起来像
LinkedList :: LinkedList() : head( nullptr ), tail( nullptr )
{
}
在这种情况下,成员函数empty应该声明为
bool empty() const;
,定义为
bool empty() const { return ( head == nullptr ); }
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 从unordered_map中删除单个节点
- 如何在单个链表中交换两个节点的位置,只修改指针
- 尝试从单向链表C++中删除单个节点时出现读取访问冲突
- 在单个 Docker 容器中开发 ROS 节点?
- 单个链表,节点插入最低到最高节点值的问题
- C++ 删除单个链表中的节点
- 在单个链接列表中间删除一个节点,仅给定对该节点的访问
- 删除单个链接列表中的m节点
- 单个链表中的 C++ 插入节点
- 在只给定单个链接列表中间的节点的情况下删除该节点.C++
- 使用递归在单个链表中查找第n到最后一个节点
- 获取单个非循环链表中指针后面节点的值
- 将单个值解析为带有容器的ast节点
- 如何在单遍历中找到单个链表的中间节点(如果链表的长度未给定)
- 数据结构-在c++中,图中单个节点的许多边
- 从双链表类打印单个节点