双链表默认typename节点

Doubly Linked List default typename Node

本文关键字:节点 typename 默认 链表      更新时间:2023-10-16

我正在写一个简单的双链表,看起来像这样:

Node.h

#pragma once
template<typename T>
class Node
{
public:
    Node() : _next(nullptr), _prev(nullptr) {}
    Node(Node* next, Node* prev, T data) : _next(next), _prev(prev), _data(data) {}
    Node* Next() { return _next; }
    Node* Prev() { return _prev; }
    T  Source() { return _data; }
    Node* _next;
    Node* _prev;
    T   _data;
};

DLList.h

#include "Node.h"
template<typename T>
class DLList
{
public:
    DLList() : _head(nullptr) {}
    Node<T>* AddNode(T val)
    {
        Node<T>* m_prev = _head;
        Node<T>* m_node = new Node<T>(nullptr, m_prev, val);
        if (m_prev)
            m_prev->_next = m_node; 
        _head = m_node;
        return m_node;
    }
    Node<T>* First()
    {
        Node<T>* m_node = _head;
        while (m_node->Prev()) { m_node = m_node->Prev(); }
        return m_node;
    }
    Node<T>* Last()
    {
        return _head;
    }
private:
    Node<T>* _head;
};

现在我对它做了一些测试,它们都很好,测试如下:

#include "DLList.h"
using namespace std;
int main(int argc, char** argv)
{
    DLList<int> testList;
    for (int i = 0; i < 10; i++)
        testList.AddNode(i * 10);
    Node<int>* node = testList.First();
    do{
        cout << "value of node : " << node->Source() << endl;
    } while (node = node->Next());
    Node<int>* end = testList.Last();
    do{
        cout << "value of node : " << end->Source() << endl;
    } while (end = end->Prev());
    cin.get();
    return 0;
}

我的问题是,每次我必须声明一个Node指针来获得列表中的第一个或最后一个Node,我必须这样做:Node<int>* end = testList.Last();Node<int>* node = testList.First();。因为我的列表已经知道类型T是否有First()Last()的可能实现,我可以只做Node* node = testList.First();Node* end = testList.Last();,而不必提供模板参数?

"由于我的列表已经知道类型T,是否有可能实现First()Last(),我可以只做Node* node = testList.First();Node* end = testList.Last(); ?"

不,你不能,但你可以使用auto关键字,让编译器推断出实际类型:

    auto node = testList.First();
 // ^^^^
    do{
        cout << "value of node : " << node->Source() << endl;
    } while (node = node->Next());

我个人不会直接暴露Node。

我将向类公开一个内部定义的Iterator类型(如果碰巧是Node,那就好了)。

template<typename T>
class DLList
{
public:
    DLList() : _head(nullptr) {}
    typedef  Node<T>        iterator;
    typedef  Node<const T>  const_iterator;
    T&       First();      // would not expose a node here.
    T const& First() const;// I would return the first value in the list.
    iterator       begin();      // Iterator to first element.
    const_iterator begin() const;
    ....

现在你可以使用auto技巧来防止你指定的东西太精确。

int main()
{
    DLList<int>     data;
    // Add data.
    auto it = data.begin();