模板实现链表队列

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
template<class T>
struct ListNode
{
	ListNode<T>* _next;
	ListNode<T>* _prev;
	T _data;

	ListNode(const T& x)
		:_data(x)
		,_next(NULL)
		,_prev(NULL)
	{}
};

template<class T>
class List
{
	typedef ListNode<T> Node;
public:
	List()
		:_head(NULL)
		,_tail(NULL)
	{}

	// l2(l1)
	List(const List<T>& l)
		:_head(NULL)
		,_tail(NULL)
	{
		Node* cur = l._head;
		while (cur)
		{
			PushBack(cur->_data);
			cur = cur->_next;
		}
	}

	// l1 = l2
	List<T>& operator=(List<T> l)
	{ 
		Swap(l);

		return *this;
	}

	void Swap(List<T>& l)
	{
		swap(_head, l._head);
		swap(_tail, l._tail);
	}

	~List()
	{
		Clear();
	}

	void Clear()
	{
		Node* cur = _head;
		while (cur)
		{
			Node* next = cur->_next;
			delete cur;
			cur = next;
		}

		_head = _tail = NULL;
	}

	void PushBack(const T& x)
	{
		if (_head == NULL)
		{
			_head = _tail = new Node(x);
		}
		else
		{
			Node* tmp = new Node(x);
			_tail->_next = tmp;
			tmp->_prev = _tail;

			_tail = tmp;
		}
	}

	void PopBack()
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head == _tail)
		{
			delete _tail;
			_head = _tail = NULL;
		}
		else
		{
			Node* prev = _tail->_prev;
			delete _tail;
			_tail = prev;
			_tail->_next = NULL;
		}
	}

	void PushFront(const T& x)
	{
	     if (_head==NULL)
	     {
			 PushBack(x);
	     }
		 else  
		 {
		      Node* tmp=new Node(x);

			  tmp->_next = _head;
			  _head->_prev = tmp;

			  _head = tmp;
		 }
	}
	void PopFront()
	{
		if (_head == NULL)
		{
			return;
		}
		else if (_head == _tail)
		{
			delete _head;
			_head = _tail = NULL;
		}
		else
		{
			Node* next = _head->_next;
			delete _head;
			_head = next;
		}
	}
	T& Front()
	{
		assert(_head);
		return _head->_data;
	}

	T& Back()
	{
		assert(_tail);
		return _tail->_data;
	}

	size_t Size()
	{
		size_t count = 0;
		Node* cur = _head;
		while (cur)
		{
			++count;
			cur = cur->_next;
		}
		return count;
	}

	bool Empty()
	{
		return _head == NULL;
	}

	void Insert(Node* pos, const T& x)
	{
	    assert(pos);
		if (pos==_head)
		{
			PushFront(x);
		}
		else if (pos==_tail)
		{
			PushBack(x);
		}
		else
		{
           Node* pos=new Node(x);
		   Node* cur=_head;
		   while (cur->_next!=pos)
		   {
			   cur=cur->_next;
		   }
		   cur->_next=pos;
		   pos->_next=cur->_next->_next;
		   pos->_prev=cur;   
		}
        
	}
	void Erase(Node* pos)
			{
				assert(pos);
				if (pos == _head)
				{
					PopFront();
				}
				else if (pos == _tail)
				{
					PopBack();
				}
				else
				{
					Node* cur = _head;
					while (cur->_next != pos)
					{
						cur = cur->_next;
					}
					cur->_next = pos->_next;
					delete pos;
				}
				return;
			}
	Node* Find(const T& x)
	{
		Node* cur=_head;
			while (cur)
			{
				if ( cur->_data==x)
				{ 
					return  cur;
				}
				cur=cur->_next;
			}
			if (!cur)
			{
				 return NULL;
			}
	}

	void Print()
	{
		Node* cur = _head;
		while(cur)
		{
			cout<<cur->_data<<" ";
			cur = cur->_next;
		}
		cout<<endl;
	}

protected:
	Node* _head;
	Node* _tail;
};
//尾插尾删
void TestList1()
{
	List<int> l;
	l.PushBack(1);
	l.PushBack(2);
	l.PushBack(3);
	l.PushBack(4);
	l.Print();
     l.PopBack();
	 l.PopBack();
	 l.PopBack();
	 l.PopBack();
	 l.PopBack();
	
}

//头插,头删
void TestList2()
{
	List<int> l;
	l.PushBack(1);
	l.PushFront(8);
	l.PushFront(9);
	l.Print();
     l.PopFront();
	 l.PopFront();
     l.PopFront();
	 l.PopFront();
    l.Print();
}
//查找,删除或插入任意一个
void TestList3()
{
	List<int> l;
	l.PushBack(1);
	l.PushFront(8);
	l.PushFront(9);
	l.Print();
	l.Find(8);
	l.Erase(l.Find(8));
	l.Print();
	l.Insert(l.Find(9), 10);
	l.Insert(l.Find(19), 10);
	l.Print();
}



// 适配器模式实现一个队列
template<class T, class Container = List<T>>
class Queue
{
public:
	void Push(const T& x)
	{
		_con.PushBack(x);
	}

	void Pop()
	{
		_con.PopFront();
	}

	T& Front()
	{
		return _con.Front();
	}

	T& Back()
	{
		return _con.Back();
	}

	size_t Size()
	{
		return _con.Size();
	}

	bool Empty()
	{
		return _con.Empty();
	}

protected:
	Container _con;
};

void TestQueue()
{
	//Queue<int, List<int>> q;
	Queue<int> q;
	q.Push(1);
	q.Push(2);
	q.Push(3);

	while (!q.Empty())
	{
		cout<<q.Front()<<" ";
		q.Pop();
	}
	cout<<endl;
} 


#include "List.h"
 
int main()
{
	 
	TestList1();
    TestList2();
	TestList3();
	TestQueue();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值