Circular queue
#include<iostream>
using namespace std;
#define n 20
class queue{
int *arr;
int front;
int back;
public:
queue(){
arr=new int[n];
front=-1;
back=-1;
}
bool isFull(){
if((back+1)%n==front){
return true;
}
else{
return false;
}
}
void push(int x){
if(isFull()){
cout<<"queue is overflow "<<endl;
return;
}
back=(back+1)%n;
arr[back]=x;
if(front==-1){
front++;
}
}
int pop(){
int x=0;
if(front==-1&&front>back){
cout<<"no element in the queue "<<endl;
return -1;
}
else if(back==front){
x=arr[back];
back=-1;
front=-1;
return x;
}
else{
x=arr[front];
arr[front]=0;
front=(front+1)%n;
return x;
}
}
bool isempty(){
if(front==-1&& front>back){
return true;
}
return false;
}
};
int main(){
queue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.pop();
q.pop();
q.pop();
q.pop();
cout<<q.isempty()<<endl;
return 0;
}
Comments
Post a Comment