从双链表类打印单个节点

Printing a Single Node from a Doubly Linked List Class

本文关键字:单个 节点 打印 链表      更新时间:2023-10-16

我试图实现三个方法目前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;
}

数据成员headtail定义为

    Node *head;
    Node *tail;

也就是说它们是指向Node的指针。因此,如果任何函数返回headtail,那么它的返回类型必须是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 ); }