//循环队列的另一种实现
#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;
}
数据结构学习之循环队列的另一种c++实现
最新推荐文章于 2020-01-03 18:35:26 发布