双向循环队列

ListNode.h

template<typename Type>class DoublyList;

template<typename Type>class ListNode{
private:
	Type m_data;
	ListNode *m_pprior;
	ListNode *m_pnext;
public:
	Type GetData();
private:
	friend class DoublyList<Type>;
	ListNode():m_pprior(NULL),m_pnext(NULL){};
	ListNode(const Type item,ListNode<Type> *prior=NULL,ListNode<Type> *next = NULL)
		:m_data(item),m_pprior(prior),m_pnext(next){}
	~ListNode(){
		m_pprior = NULL;
		m_pnext = NULL;
	}
	Type GetData(){return m_data;}
};

DoublyList.h

#include "ListNode.h"

template<typename Type>class DoublyList{
public:
	DoublyList():head(new ListNode<Type>){
		head->m_pprior = head;
		head->m_pnext = head;
	}
	~DoublyList(){
		MakeEmpty();
		delete head;
	}

public:
	void MakeEmpty();
	int Length();
	ListNode<Type> *Find(int n = 0);
	ListNode<Type> *FindData(Type item);
	bool Insert(Type item,int n = 0);
	Type Remove(int n = 0);
	Type Get(int n = 0);
	void Print();
private:
	ListNode<Type> *head;
};

DoublyList.cpp

template<typename Type> void DoublyList<Type>::MakeEmpty(){
	ListNode<Type>* p;
	while(head->m_pnext != head)
	{
		p = head->m_pnext;
		head->m_pnext = p->m_pnext;
		p->m_pnext->m_pprior = head;
		delete p;
	}
}

template<typename Type> int DoublyList<Type>::Length(){
	int count = 0;
	ListNode<Type>* p = head;
	while(p->m_pnext != head)
	{
		p = p->m_pnext;
		count++;
	}
	return count;
}
template<typename Type> ListNode<Type>* DoublyList<Type>::Find(int n = 0){
	if(n < 0)
	{
		cout<<"The n is out of boundary"<<endl;
		return NULL;
	}
	int count = 0;
	ListNode<Type> *p = head;
	while(p->m_pnext != head)
	{
		p=p->m_pnext;
		if(count == n)
			return p;
		count++;
	}
	cout<<"The n is out of boundary"<<endl;
	return NULL;
}

template<typename Type> bool DoublyList<Type>::Insert(Type item,int n){
	if(n < 0)
	{
		cout<<"The n is out of boundary"<<endl;
		return 0;
	}
	int  count = 0;
	ListNode<Type> *p = head;
	while(1)
	{
		if(count == n)
		{
			ListNode<Type> *q = new ListNode<Type>(item,p,p->m_pnext);
			p->m_pnext->m_pprior = q;
			p->m_pnext = q;
			return 1;
		}
		p = p->m_pnext;
		count++;
		if(p == head)
			break;
	}
	cout<<"The n is out of boundary"<<endl;
	return 0;
}

template<typename Type> Type DoublyList<Type>::Remove(int n = 0){
	if(n < 0)
	{
		cout<<"The n is out of boundary"<<endl;
		return 0;
	}
	int  count = 0;
	ListNode<Type> *p = head;
	while(1)
	{
		if(count == n)
		{
			ListNode<Type> *q = p->m_pnext;
			p->m_pnext = q->m_pnext;
			p->m_pnext->m_pprior = p;
			Type temp = q->GetData();
			delete q;
			return temp;
		}
		p = p->m_pnext;
		count++;
		if(p == head)
			break;
	}
	cout<<"The n is out of boundary"<<endl;
	return 0;
}

template<typename Type> Type DoublyList<Type>::Get(int n = 0){
	if(n < 0)
	{
		cout<<"The n is out of boundary"<<endl;
		return 0;
	}
	int  count = 0;
	ListNode<Type> *p = head;
	while(p->m_pnext != head)
	{
		p = p->m_pnext;
		if(count == n)
			return p->GetData();
		count++;
	}
	cout<<"The n is out of boundary"<<endl;
	return 0;
}

template<typename Type> void DoublyList<Type>::Print(){
	ListNode<Type> *p = head;
	while(p->m_pnext != head)
	{
		p = p->m_pnext;
		cout<<p->GetData()<<' ';
	}
	cout<<endl;
}

template<typename Type> ListNode<Type>* DoublyList<Type>::FindData(Type item){
	ListNode<Type> *p = head;
	while(p->m_pnext != head)
	{
		p = p->m_pnext;
		if(p->GetData() == item)
			return p;
	}
	cout<<"can't find the element"<<endl;
	return 0;
}

Test.cpp

#include <iostream>
#include "DoubleList.h"
using namespace std;

int main()
{
	DoublyList<int> list;
	for(int i=0;i<20;i++){
		list.Insert(i*3,i);
	}
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();
	for(int i=0;i<5;i++){
		list.Insert(3,i*3);
	}
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	list.Remove(5);
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	cout<<list.FindData(54)->GetData()<<endl;

	cout<<"The third element is "<<list.Get(3)<<endl;

	list.MakeEmpty();
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	return 0;
}

转载于:https://my.oschina.net/windmissing/blog/690450

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值