Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Queue{
- int size,f,r,cnt;
- int*a;
- public:
- Queue(int n){
- size=n;
- f=-1;
- r=-1;
- cnt=0;
- a=new int[n];
- }
- void push_front(int x){
- if(cnt==size)cout<<"FULL\n";
- else{
- if(f==-1){
- f=r=0;
- a[f]=x;
- }
- else{
- if(f==0)f=size-1;
- else f--;
- a[f]=x;
- }
- cnt++;
- cout<<"push: "<<x<<endl;
- }
- }
- void push_back(int x){
- if(cnt==size)cout<<"FULL\n";
- else{
- if(f==-1){
- f=r=0;
- a[f]=x;
- }
- else{
- if(r==size-1)r=0;
- else r++;
- a[r]=x;
- }
- cnt++;
- cout<<"push: "<<x<<endl;
- }
- }
- int pop_front(){
- if(cnt==0)cout<<"Empty\n";
- else{
- int x=a[f];
- if(f==r)f=r=-1;
- else f=(f+1)%size;
- cnt--;
- return x;
- }
- }
- int pop_back(){
- if(cnt==0)cout<<"Empty\n";
- else{
- int x=a[r];
- if(f==r)f=r=-1;
- else if(r==0)r=size-1;
- else r--;
- cnt--;
- return x;
- }
- }
- int front(){
- if(cnt==0)return -1;
- else return a[f];
- }
- int rear(){
- if(cnt==0)return -1;
- else return a[r];
- }
- void show(){
- if(f==-1)cout<<"empty";
- else if(f<=r)for(int i=f;i<=r;i++){
- cout<<a[i]<<" ";
- }
- else{
- for(int i=f;i<size;i++)cout<<a[i]<<" ";
- for(int i=0;i<=r;i++)cout<<a[i]<<" ";
- }
- cout<<endl;
- }
- };
- int main()
- {
- Queue q(6);
- q.show();
- q.push_back(1);
- q.show();
- q.push_back(2);
- q.show();
- q.push_front(5);
- q.show();
- q.push_front(6);
- q.show();
- q.push_back(3);
- q.show();
- q.push_back(4);
- q.show();
- q.push_front(7);
- q.show();
- q.push_front(8);
- q.show();
- q.pop_back();
- q.show();
- q.pop_front();
- q.show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement