c_cpp 使用数组实现循环队列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 使用数组实现循环队列相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
#define MAX_SIZE 10 //defining Queue's maximum size
using namespace std;

// #Queue #BasicProblem
// Resetting queue if it becomes empty after a Deq operations is important

bool isFull(int frontIndex, int backIndex){
    if(frontIndex==0 && backIndex==MAX_SIZE-1){
        return true;    // normal case
    }else if(backIndex+1==frontIndex){
        return true;    //if backIndex is right behind frontIndex
    }else{
        return false;
    }
}
bool isEmpty(int frontIndex,int backIndex){
    if(frontIndex==-1 && backIndex==-1){  //as we always reset the queue if a Deq makes it empty
        return true;
    }else{
        return false;
    }
}
bool Enq(int Q[],int push,int &frontIndex,int &backIndex){
    if(isFull(frontIndex,backIndex)==true){
        return false;
    }else{
        if(backIndex==-1){
            frontIndex=0; // starting to enqueue hence changing front from -1 to 0
        }
        int r=(backIndex+1)%MAX_SIZE; //logic for circular rotating array
        Q[r]=push;
        backIndex=r; //new backIndex
        return true;
    }
}
bool Deq(int Q[],int &frontIndex,int &backIndex,int &removed){
    if(isEmpty(frontIndex,backIndex)==true){
        return false;
    }else if(frontIndex==backIndex){
        removed=Q[frontIndex];	// resetting the queue if there is a single element
        frontIndex=-1;
        backIndex=-1;
        return true;
    }else{
        removed=Q[frontIndex];
        frontIndex=(frontIndex+1)%MAX_SIZE; //making next element of queue as the front
        return true;
    }
}

int main() {
    cout<<"Instructions: \n";
    cout<<"Type add to Enqueue"<<endl;
    cout<<"Type del to Dequeue"<<endl;
    cout<<"Type front to check front element"<<endl;
    cout<<"Type exit to stop using the Queue"<<endl;
    int Q[MAX_SIZE];
    int frontIndex=-1; // initializing an empty Queue
    int backIndex=-1;
    int removed; // variable to access the element dequeued
    while(1){
        string instruction;
        cout<<"Instruction: ";
        cin>>instruction;
        if(instruction=="exit"){
            break;
        }else if(instruction=="add"){
            cout<<"Enter the element top be enqueued"<<endl;
            int push; //element to be enqueued
            cin>>push;
            if(Enq(Q,push,frontIndex,backIndex)==true){
                cout<<"Element "<<Q[backIndex]<<" successfully enqueued"<<endl;
            }else{
                cout<<"ERROR: Queue is already full!"<<endl;
            }
        }else if(instruction=="del"){
            if(Deq(Q,frontIndex,backIndex,removed)==true){
                cout<<"Element "<<removed<<" successfully Dequeued"<<endl;
                if(isEmpty(frontIndex,backIndex)==false){
                        cout<<"Front Element is:"<<Q[frontIndex]<<endl;
                }else{
                    cout<<"Queue is now Empty!"<<endl;
                }
            }else{
                cout<<"ERROR : Queue is empty!"<<endl;
            }
        }else if(instruction=="front"){
            if(isEmpty(frontIndex,backIndex)==false){
                cout<<"Front Element is:"<<Q[frontIndex]<<endl;
            }else{
                cout<<"Queue is now Empty!"<<endl;
            }
        }else{
            cout<<"ERROR : Unknown operation! Please try again"<<endl;
        }
    }
    return 0;
}

以上是关于c_cpp 使用数组实现循环队列的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C ++中的队列数组实现

C语言实现使用动态数组实现循环队列

go语言循环队列的实现

java使用数组实现循环队列

java使用数组实现循环队列

数据结构:循环队列