有点奇怪的是,不久前我成功地实现了我自己的模板链表作为一项教育练习。现在我知道它有效,我一直在使用它来创建一个包含“Sprite”对象的节点列表,当时我理解它,但我今天去回顾它,我真的很困惑。
如果你愿意看看我的实现并帮助我弄清楚。
程序如何知道应该为 _data(在 Node 中)分配一个 NodeType 对象?(如果你会原谅糟糕的措辞)我的意思是我有那个功能SetData
,但我什至从不使用它。例如,当我在列表上 PushBack 时,我只是创建了一个 ListType 的新节点,但我从未设置该节点的数据。节点构造函数没有说_data = new NodeType
什么。
然而,我可以调用GetData()
列表中的任何节点并返回一个 Sprite 对象。我的意思是我知道它有效,但我忘记了如何!有人可以看看并提醒我吗?将不胜感激!
节点.hpp:
#if !defined NODE_HPP
#define NODE_HPP
template<typename NodeType>
class Node{
public:
Node( Node* prev = 0, Node* next = 0);
void SetData(NodeType newData);
NodeType* GetData();
Node* GetPrev();
Node* GetNext();
private:
template<typename ListType>
friend class List;
NodeType _data;
Node* _prev;
Node* _next;
};
///implement Node
template <typename NodeType>
Node<NodeType>::Node(Node* prev, Node* next) : _prev(prev), _next(next)
{}
template <typename NodeType>
void Node<NodeType>::SetData(NodeType newData)
{
_data = newData;
}
template <typename NodeType>
NodeType* Node<NodeType>::GetData()
{
return &_data;
}
template <typename NodeType>
Node<NodeType>* Node<NodeType>::GetPrev()
{
return _prev;
}
template <typename NodeType>
Node<NodeType>* Node<NodeType>::GetNext()
{
return _next;
}
#endif //define
列表.hpp
#if !defined LIST_HPP
#define LIST_HPP
#include "Node.hpp"
///since we're creating a template everything must be defined in the hpp
template <typename ListType>
class List
{
public:
List();
bool Empty();
void PushFront();
void PushBack();
void PopBack();
void ClearList();
Node<ListType>* GetHead();
Node<ListType>* GetTail();
int NumberOfNodes();
private:
Node<ListType>* _head;
Node<ListType>* _tail;
int _size;
};
///implement List class here
template <typename ListType>
List<ListType>::List() : _head(0), _tail(0), _size(0)
{
}
template <typename ListType>
bool List<ListType>::Empty()
{
return _size == 0;
}
template <typename ListType>
void List<ListType>::PushFront()
{
_head = new Node<ListType>( _head , 0 );
if(Empty()) //this is the start of a new list, need to set _tail as well
_tail = _head;
else
_head->_prev->_next = _head; //set previous nodes _next to new _head
++_size;
}
template <typename ListType>
void List<ListType>::PushBack()
{
_tail = new Node<ListType>( 0 , _tail);
if(Empty()) //this is the start of a new list, need to set _head as well
_head = _tail;
else
_tail->_next->_prev = _tail; // set old tails _prev to new tail
++_size;
}
template <typename ListType>
void List<ListType>::PopBack()
{
if(!Empty()){
if(_tail != _head)
{
_tail = _tail->_next;
delete _tail->_prev; //delete memory at old tail
_tail->_prev = 0; //reassign new tails _prev pointer to null
}
else // We are deleting the last node so treatment is a little different.
{
delete _tail;
_tail = 0; _head = 0;
}
--_size;
}
}
template <typename ListType>
void List<ListType>::ClearList()
{
while(!Empty())
PopBack();
}
template <typename ListType>
Node<ListType>* List<ListType>::GetHead()
{
return _head;
}
template <typename ListType>
Node<ListType>* List<ListType>::GetTail()
{
return _tail;
}
template <typename ListType>
int List<ListType>::NumberOfNodes()
{
return _size;
}
#endif //define