使用链式结构实现队列

/******************************************************************************************
Copyright (c) Inc.(2016), All rights reserved.

Purpose:
// 1:直接使用链式结构实现队列
// 2:使用链表实现适配器模式的的队列

Author: MaJing

Created Time: 2016-9
******************************************************************************************/

#pragma once
#include<assert.h>

template<class T>
class Queue
{
	struct Node
	{
		Node* _next;
		T _data;

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

public:
	Queue()
		:_head(NULL)
		,_tail(NULL)
		,_size(0)
	{}

	~Queue()
	{
		Clear();
	}

	// 拷贝构造和赋值运算符的重载需要实现,并考虑深拷贝浅拷贝问题。(作业完成)

	void Push(const T& x)
	{
		//
		// 1.没有节点
		// 2.有节点
		//
		if (_head == NULL)
		{
			_head = new Node(x);
			_tail = _head;
		}
		else
		{
			_tail->_next = new Node(x);
			_tail = _tail->_next;
		}

		++_size;
	}

	void Pop()
	{
		assert(_head);

		//
		// 1.最后一个节点
		// 2.一个以上节点
		//
		if (_head == _tail)
		{
			delete _head;
			_head = NULL;
			_tail = NULL;
		}
		else
		{
			Node* tmp = _head;
			_head = _head->_next;
			delete tmp;
		}

		--_size;

	}

	const T& Back()
	{
		assert(_tail);

		_tail->_data;
	}

	const T& Front()
	{
		assert(_head);

		return _head->_data;
	}

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

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

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

		_head = NULL;
		_tail = NULL;
		_size = 0;
	}

	size_t Size()
	{
		return _size;
	}

private:
	Node*  _head;
	Node*  _tail;

	int _size;
};

void TestQueue()
{
	Queue<int> q;
	q.Push(1);
	q.Push(2);
	q.Push(3);
	q.Push(4);
	q.Push(5);

	q.Print();

	q.Pop();
	q.Pop();
	q.Pop();
	q.Pop();

	q.Print();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值