这几天一直在忙一个php项目,还有学校本科评估,不敢逃课,好久double没空写代码了。今天抽空写了书上的循环队列
代码如下:
//seqCircleQueue.cpp
#include<iostream>
using namespace std;
template<typename T>
class seqCircleQueue{
public:
seqCircleQueue();
~seqCircleQueue();
bool Enqueue( T elem );
T Dequeue();
bool isEmpty();
bool isFull();
private:
static const int size = 10;
T *res;
int rear;
int front;
};
template<typename T>
seqCircleQueue<T>::seqCircleQueue(){
res = new T[size];
front = 0;
rear = 0;
}
template<typename T>
seqCircleQueue<T>::~seqCircleQueue(){
delete[] res;
}
template<typename T>
bool seqCircleQueue<T>::isEmpty(){
if( front == rear){
return true;
}else{
return false;
}
}
template<typename T>
bool seqCircleQueue<T>::isFull(){
if( (rear + 1)%size == front ){
return true;
}else{
return false;
}
}
template<typename T>
bool seqCircleQueue<T>::Enqueue(T elem){
if( isFull() ){
return false;
}else{
res[rear] = elem;
rear = (rear+1)%size;
}
}
//默认不为空
template<typename T>
T seqCircleQueue<T>::Dequeue(){
T q;
q = res[front];
front = ( front + 1)%size;
return q;
}
//main.cpp
#include"seqCircleQueue.cpp"
int main()
{
seqCircleQueue<int> t;
if( t.isEmpty() ){
cout<<"isempty"<<endl;
}
t.Enqueue( 11 );
t.Enqueue( 22 );
t.Enqueue( 33 );
t.Enqueue( 44 );
t.Enqueue( 55 );
t.Enqueue( 66 );
t.Enqueue( 77 );
t.Enqueue( 88 );
t.Enqueue( 99 );
if( t.isFull() ){
cout<<"isfull"<<endl;
}
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
t.Enqueue( 55 );
t.Enqueue( 66 );
t.Enqueue( 77 );
t.Enqueue( 88 );
t.Enqueue( 99 );
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
cout<< t.Dequeue() <<endl;
}