单链队列,队列的链式存储结构C++实现

//mList.cpp
/*
	使用链式结构实现队列操作
	@author:天下无双
	@date:2014-5-28
	@version:2.0
*/
#include <iostream>
using namespace std;
template<class T>
class Queue
{
private:
	static const int MAX=100 ;//队列的最大长度
	//队列节点
	struct QNode{
	T data;//节点值
	QNode *next;
	};
	QNode *front;//队列头指针
	QNode *rear;//队列尾指针
	int count;//队列长度
public:
	//创建空队列
	Queue(){
		front=rear=nullptr;
		count=0;
	}
	~Queue(){
		QNode *temp=front;
		QNode *p=temp;
		cout<<endl;
		while(temp!=nullptr){
			temp=temp->next;
			cout<<"delete "<<p->data<<endl;
			delete p;
			p=temp;		
		}
	}
	//插入元素
	bool EnQueue(T &t){
		QNode *p=new QNode;
		p->data=t;
		p->next=nullptr;
		if(nullptr==front){//如果是一个空队列
			front=rear=p;
			count++;
			front->next=nullptr;
			return true;
		}else if(!isFull()){//判断队列是否已满
		rear->next=p;
		rear=p;//重新指定尾指针,rear=p
		//rear->next=nullptr;//重新指定尾指针,rear=p->next=nullptr;
		count++;
		return true;
		}else
			return false;
			
	}
	//删除队列头
	bool DeleteQueue(){
		if(isEmpty()){//判断是否为空队列
			cout<<"Queue is Empty!Can't delte it!"<<endl;
			return false;
		}
		QNode *temp=front;
		front=front->next;
		delete temp;
		count--;
		return true;

	}
	int Length()const{
		return count;
	}
	void show()const{
		QNode *temp=front;
		while(temp!=nullptr){
			cout<<temp->data<<" ";
			temp=temp->next;
		}
	}
protected:
	bool isEmpty()const{
		return count==0;
	}
	bool isFull()const{
		if(count==MAX){
			cout<<"Queue full!"<<endl;
			return true;
		}
		return false;
	}




};
测试代码:
<pre name="code" class="cpp">int main()
{
	{
	Queue<int> q;
	for(int i=1;i<103;i++)
		q.EnQueue(i);
	cout<<endl;
	q.show();
	cout<<endl;
	for(int j=0;j<102;j++)
	  q.DeleteQueue();
	}
	system("pause");
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值