数据结构与算法C++实现(9)之双线链表

这个双向列表的首节点的数据其实是空的,这是为了让操作更加简单。

代码:

#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!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值