0

我正在研究一个双向链表类。我已经实现了一个 Node 类,它包含一个指向下一个元素的指针和一个指向前一个元素的指针以及一个数据部分(类型由模板参数“Type”定义)

最初,我将其实现为两个类。一个类定义了一个节点,另一个定义了节点的链表(rList)。我想将 Node 类模板作为 rList 的私有成员包含在内,因为节点应该隐藏在列表中并且只能由 rlist 管理。

Node 类只有一个构造函数,用于初始化数据部分并将指针设置为空。

我试图将 Node 类放在 rList 类的私有部分,但我似乎无法让组织正确。我被语法错误困扰。下面是我目前的实现。

template<typename Type>
class Node
{
    Node* next;
    Node* prev;

public:
    Node(Type);
    template<typename Type> friend class rList;
    Type data;
};

template<typename Type>
Node<Type>::Node(Type d):data(d), next(0), prev(0)
{}


template<typename Type>
class rList
{
private:
    Node<Type>* head;
    Node<Type>* tail;
    int size;

public:

    rList<Type>();
    ~rList();
    void push_front(const Type&);
    void push_back(const Type&);
    void insert(Node<Type>*, const Type&); //needs to be changed after merge
    void erase(Node<Type>*); //needs to be changed after merge
    void clear();
};

template<typename Type>
rList<Type>::rList():head(0), tail(0), size(0)
{}


template<typename Type>
rList<Type>::~rList()
{
    clear();
}


template<typename Type>
void rList<Type>::clear()
{
    while(head != 0)
    {
        Node<Type>* nextNode = head->next;
        delete head;
        head = nextNode;
    }
}


template<typename Type>
void rList<Type>::push_front(const Type& data)
{
    Node<Type>* newOne = new Node<Type>(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else
    {
        newOne->next = head;
        head = newOne;
    }
    size++;
}


template<typename Type>
void rList<Type>::push_back(const Type& data)
{
    Node<Type>* newOne = new Node<Type>(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else
    {
        tail->next = newOne;
        newOne->prev = tail; 
        tail = newOne;
    }
    size++;
}


template<typename Type>
void rList<Type>::insert(Node<Type>* itr, const Type& data)
{
    Node<Type>* newOne = new Node<Type>(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else 
    {   
        newOne->next = itr;
        (itr->prev)->next = newOne;
        newOne->prev = itr->prev;
        itr->prev = newOne;
    }
    size++;
}


template<typename Type>
void rList<Type>::erase(Node<Type>* itr)
{
    if(itr == tail)
    {
        tail = itr->prev;
        tail->next = 0;
        delete itr;
    }

    else if(itr == head)
    {
        head = itr->next;
        head->prev = 0;
        delete itr;
    }

    else
    {
        (itr->prev)->next = itr->next;
        (itr->next)->prev = itr->prev;
        delete itr;
    }
    size--;
}


template<typename Type>
Node<Type>* rList<Type>::begin()
{
    return head;
}


template<typename Type>
Node<Type>* rList<Type>::end()
{
    return tail;
}


template<typename Type>
Node<Type>* rList<Type>::nextElem(Node<Type>* node)
{
    return node->next;
}

我意识到这会破坏一些事情,因为 Node* 不能在 rList 类之外定义。我计划稍后使用迭代器类更改它以访问 rList 中的节点。但首先,如何使用这种排列方式组织模板和成员函数定义?下面显示了我的尝试:

template<typename Type>
class rList //rList class template
{
private:
    template<typename Type>
    class Node //Node class template
    {
        Node* next;
        Node* prev;

    public:
        Type data; //can be any POD type or class/struct
        Node(Type);
        template<typename Type> friend class rList; //so rList can access private Node pointers
    };
    Node<Type>* head;
    Node<Type>* tail;
    int size;

public:
    rList<Type>();
    ~rList();
    void push_front(const Type&);
    void push_back(const Type&);
    void insert(Node<Type>*, const Type&);
    void erase(Node<Type>*);
    clear();
};

template<typename Type>
rList<Type>::rList():head(0), tail(0), size(0)
{}


template<typename Type>
rList<Type>::~rList()
{
    clear();
}

template<typename Type>
void rList<Type>::clear()
{
    while(head != 0)
    {
        Node<Type>* nextNode = head->next;
        delete head;
        head = nextNode;
    }
}

template<typename Type>
void rList<Type>::push_front(const Type& data)
{
    Node<Type>* newOne = new Node<Type>(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else
    {
        newOne->next = head;
        head = newOne;
    }
    size++;
}

template<typename Type>
void rList<Type>::push_back(const Type& data)
{
    Node<Type>* newOne = new Node<Type>(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else
    {
        tail->next = newOne;
        newOne->prev = tail; 
        tail = newOne;
    }
    size++;
}

template<typename Type>
void rList<Type>::insert(Node<Type>* itr, const Type& data)
{
    Node<Type>* newOne = new Node<Type>(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else 
    {   
        newOne->next = itr;
        (itr->prev)->next = newOne;
        newOne->prev = itr->prev;
        itr->prev = newOne;
    }
    size++;
}


template<typename Type>
void rList<Type>::erase(Node<Type>* itr)
{
    if(itr == tail)
    {
        tail = itr->prev;
        tail->next = 0;
        delete itr;
    }

    else if(itr == head)
    {
        head = itr->next;
        head->prev = 0;
        delete itr;
    }

    else
    {
        (itr->prev)->next = itr->next;
        (itr->next)->prev = itr->prev;
        delete itr;
    }
    size--;
}


//constructor for Node
template<typename Type>
rList<Type>::Node<Type>::Node(Type d):data(d), next(0), prev(0)
{}

感谢您的意见!

* 更新代码 * 下面的代码是根据建议修改的。它正在工作并且似乎隐藏了适当的成员。请评论您注意到的任何改进!再次感谢!!

template<typename Type>
class rList
{
private:

    class Node
    {
        Node* next;
        Node* prev;

    public:
        Type data;
        Node(Type d);
        template<typename Type> friend class rList;
    };

    Node* head;
    Node* tail;
    int size;

public:
    rList<Type>();
    ~rList();
    void push_front(const Type&);
    void push_back(const Type&);
    void insert(Node*, const Type&);
    void erase(Node*);
    void clear();
};

template<typename Type>
rList<Type>::rList():head(0), tail(0), size(0)
{}


template<typename Type>
rList<Type>::~rList()
{
    clear();
}

template<typename Type>
void rList<Type>::clear()
{
    while(head != 0)
    {
        Node* nextNode = head->next;
        delete head;
        head = nextNode;
    }
}

template<typename Type>
void rList<Type>::push_front(const Type& data)
{
    Node* newOne = new Node(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else
    {
        newOne->next = head;
        head = newOne;
    }
    size++;
}

template<typename Type>
void rList<Type>::push_back(const Type& data)
{
    Node* newOne = new Node(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else
    {
        tail->next = newOne;
        newOne->prev = tail; 
        tail = newOne;
    }
    size++;
}

template<typename Type>
void rList<Type>::insert(Node* itr, const Type& data)
{
    Node* newOne = new Node(data);

    if(head == 0)
    {
        head = newOne;
        tail = head;
    }
    else 
    {   
        newOne->next = itr;
        (itr->prev)->next = newOne;
        newOne->prev = itr->prev;
        itr->prev = newOne;
    }
    size++;
}


template<typename Type>
void rList<Type>::erase(Node* itr)
{
    if(itr == tail)
    {
        tail = itr->prev;
        tail->next = 0;
        delete itr;
    }

    else if(itr == head)
    {
        head = itr->next;
        head->prev = 0;
        delete itr;
    }

    else
    {
        (itr->prev)->next = itr->next;
        (itr->next)->prev = itr->prev;
        delete itr;
    }
    size--;
}

template<typename Type>
rList<Type>::Node::Node(Type d):data(d), next(0), prev(0)
{}
4

0 回答 0