这个双向列表的首节点的数据其实是空的,这是为了让操作更加简单。
代码:
#ifndef _LIST_H
#define _LIST_H
template<class T> class Node;//声明
template<class T> class _iterator;
template<class T>
class List
{
public:
typedef _iterator<T> iterator;
friend class Node<T>;
public:
friend class Node<T>;
List();
bool IsEmpty();//判断链表是否为空
void Insert(iterator cur_iterator, T item);//在指定的迭代器的后面加入一个元素
void push_back(T item);//在链表的后面加入一个元素
iterator begin();//返回开始的迭代器
iterator end();//返回结束的迭代器
private:
Node<T>* P_firstnode;//指向第一个节点
};
template<class T>
List<T>::List()
{
Node<T>* P_newNode = new Node<T>;//建立一个空节点,自己指向自己
this->P_firstnode = P_newNode;
P_newNode->P_nexNode = P_newNode->P_preNode = P_newNode;
}
template<class T>
inline bool List<T>::IsEmpty()//如果首节点自己指向自己,则证明为空
{
return this->P_firstnode->P_nexNode == this->P_firstnode->P_preNode;
}
template<class T>
void List<T>::Insert(iterator cur_iterator, T item)
{
Node<T>* P_newNode = new Node<T>(item);
P_newNode->P_preNode = cur_iterator.P_thisNode;
P_newNode->P_nexNode = cur_iterator.P_thisNode->P_nexNode;
cur_iterator.P_thisNode->P_nexNode = P_newNode;
(cur_iterator++)->P_preNode = P_newNode;//这里的“++”、“->”都进行了重载
}
template<class T>
void List<T>::push_back(T item)
{
Node<T>* P_newNode = new Node<T>(item);
if (IsEmpty())//判断是否为空
{
this->P_firstnode->P_nexNode = P_newNode;
this->P_firstnode->P_preNode = P_newNode;
P_newNode->P_preNode = this->P_firstnode;
P_newNode->P_nexNode = this->P_firstnode;
}
else//如果不为空,就调用Insert().用已有的方法,来适配新的方法是一种很不错的方法
{
this->Insert(this->end(), item);
}
}
template<class T>
inline _iterator<T> List<T>::begin()
{
return _iterator<T>(this->P_firstnode->P_nexNode);
}
template<class T>
inline _iterator<T> List<T>::end()
{
return _iterator<T>(this->P_firstnode->P_nexNode);
}
/***************************************************************************/
template<class T>
class Node
{
public:
friend class _iterator<T>;
friend class List<T>;
public:
Node(T item) : data(item) { }
Node(){ }
private:
Node<T>* P_preNode;
Node<T>* P_nexNode;
T data;
};
/***************************************************************************/
template<class T>
class _iterator
{
public:
friend class List<int>;
_iterator(Node<T>* P_Node) : P_thisNode(P_Node){ }
T operator* ();
Node<T>* operator-> ();
_iterator<T>& operator++ ();
_iterator<T> operator++ (int);
private:
Node<T>* P_thisNode;
};
template<class T>
inline T _iterator<T>::operator* ()
{
return P_thisNode->data;
}
template<class T>
inline Node<T>* _iterator<T>::operator-> ()
{
return P_thisNode;
}
template<class T>
_iterator<T>& _iterator<T>::operator++ ()
{
P_thisNode = P_thisNode->P_nexNode;
return *this;
}
template<class T>
_iterator<T> _iterator<T>::operator++ (int)
{
_iterator<T> tem = *this;
++* this;
return tem;
}
#endif
#include <iostream>
#include <vector>
#include "DbList.h"
int main()
{
List<int> mylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(2);
List<int>::iterator it = mylist.begin();
std::cout << *it << std::endl;
std::cout << *it << std::endl;
std::cout << "Hello World!\n";
}
结果:
2
2
Hello World!