【问题标题】:Printing a Single Node from a Doubly Linked List Class从双向链表类打印单个节点
【发布时间】:2014-03-17 23:11:37
【问题描述】:

我目前正在尝试实现三个方法,即 get_first()、get_last() 和 print_node()。 get_first() 将返回列表的头部,get_last() 是尾部,而 print_node() 将仅打印发送给它的节点的数据字段。我正在尝试实施,但我所做的任何更改都会不断出现指针错误。

这是我的 node.h 标头:

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.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() 函数中返回头部有关。请原谅帖子的长度。

【问题讨论】:

  • 函数成员的签名与他们的定义不匹配

标签: c++ doubly-linked-list


【解决方案1】:

您在函数声明中返回 Node*,但在定义中您将 Node 作为返回类型。用这个

Node* LinkedList :: get_first()
{
    return head;
}

Node* LinkedList :: get_last()
{
    return tail;
}

【讨论】:

    【解决方案2】:

    数据成员 headtail 定义为

        Node *head;
        Node *tail;
    

    也就是说,它们是指向 Node.js 的指针。因此,如果任何函数返回headtail,则其返回类型必须为Node * 所以这些成员函数定义

    Node LinkedList :: get_first()
    {
        return head;
    }
    
    Node LinkedList :: get_last()
    {
        return tail;
    }
    

    错了。它们返回 head 和 tail 但没有返回类型 Node * 并且它们的定义与类中的 theor 声明不一致。

    构造函数定义也是错误的。它应该看起来像

    LinkedList :: LinkedList() : head( nullptr ), tail( nullptr )
    {
    }
    

    在这种情况下,成员函数empty应该声明为

    bool empty() const;
    

    并定义为

    bool empty() const { return ( head == nullptr ); }
    

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多