数据结构学习之循环队列的另一种c++实现

//循环队列的另一种实现
#include <iostream>
#include <string>
using namespace std;
template <class Telem>
class  SqQueue //:public Queue<Telem>
{  
	Telem *elem;
	int front,rear,count;
	const int len;
   public:
	   SqQueue(int maxsz=12):len(maxsz)
	   {
		   elem=new Telem[len];
		   front=rear=0;
		   count=0;
	   };
	   ~SqQueue() {delete []elem; };
	   void init(){front=rear=0;count=0;};
	   int  leng(){ return count;};
	   bool empt(){return (count==0);};
	   bool full(){return (count >= len);};
	   bool enque (Telem& el);  //作为作业
	   Telem dlque(); //作为作业
	   Telem getf();
};
template<class Telem> bool SqQueue<Telem>::enque(Telem& el){


	if(count>=len)
		return false;
	else{

		elem[rear]=el;
		rear=(rear+1)%len;
		count++;
		return (true);
	}
};
template <class Telem> Telem SqQueue<Telem>::dlque(){


	Telem el;
	if(count==0)
		return NULL;
	else{
		el=elem[front];
		front=(front+1)%len;
		count--;
		return el;
	}
};
template<class Telem> Telem SqQueue<Telem>::getf(){


	if(count==0)
		return NULL;
	else{
		return elem[front];
	}
};

void show(SqQueue<string> queue){

	SqQueue <string> data;
	string elem;
	while(!queue.empt()){
		elem=queue.dlque();
		cout<<elem<<" ";
		data.enque(elem);
	}
	cout<<endl;

	queue.init();
	while(!data.empt()){

		elem=data.dlque();
		queue.enque(elem);
	}


}

int main(){
	SqQueue<string> q;
	string str="123";

	q.enque(str);
	str="456";
	q.enque(str);
	str="789";
	q.enque(str);
	str="abc";
	q.enque(str);
	str="efg";
	q.enque(str);
	cout<<q.dlque()<<endl;
	str="j";
	q.enque(str);
	string elem;
	while(!q.empt()){
		elem=q.dlque();
		cout<<elem<<" ";
	}
	cout<<endl;
	
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值