Data Structure: Implementation of Queue in C++

A queue is an order collection of items from which items may be deleted at one end (called front or head of the queue) and into which items may be inserted at the other end (called the rear end  or tail of the queue). It is First-in-First-out (FIFO) type of data structure. Operations on queue are: Create Queue, insert items , remove items, display etc.

Algorithm for Implementation of Queue in C++

1. Declare and initialize neccessary variables, front = 0, rear = -1 etc.
2. For enque operation,
If rear >= MAXSIZE - 1
print "Queue is full"
Else
- Increment rear by 1 i.e. rear = rear + 1;
- queue[rear] = item;
3. For next enqueue operation, goto step 2.
4. For dequeue operation
If front > rear
print "Queue is Empty"
Else
- item = queue[front]
- increment front by 1 i.e. front = front + 1
5. For dequeue next data items, goto step 4.
6. Stop

Source Code:

 
#include
 
#include
 
#define MAX_SIZE 10
 
 
 
using namespace std;
 
 
 
class Queue{
 
    private:
 
        int item[MAX_SIZE];
 
        int rear;
 
        int front;
 
    public:
 
        Queue();
 
        void enqueue(int);
 
        int dequeue();
 
        int size();
 
        void display();
 
        bool isEmpty();
 
        bool isFull();
 
 
 
 
 
};
 
Queue::Queue(){
 
    rear = -1;
 
    front = 0;
 
}
 
 
 
void Queue::enqueue(int data){
 
        item[++rear] = data;
 
}
 
 
 
int Queue::dequeue(){
 
        return item[front++];
 
}
 
 
 
void Queue::display(){
 
    if(!this->isEmpty()){
 
        for(int i=front; i
 
            cout
 
    }else{
 
        coutQueue Underflow"
 
    }
 
}
 
 
 
 
 
int Queue::size(){
 
    return (rear - front + 1);
 
}
 
bool Queue::isEmpty(){
 
    if(front>rear){
 
        return true;
 
    }else{
 
        return false;
 
    }
 
}
 
bool Queue::isFull(){
 
    if(this->size()>=MAX_SIZE){
 
        return true;
 
    }else{
 
        return false;
 
    }
 
}
 
 
 
 
 
 
 
int main(){
 
 
 
    Queue queue;
 
    int choice, data;
 
    while(1){
 
        cout\n1. Enqueue\n2. Dequeue\n3. Size\n4. Display all element\n5. Quit";
 
        cout\nEnter your choice: ";
 
        cin>>choice;
 
        switch(choice){
 
            case 1:
 
            if(!queue.isFull()){
 
                    cout\nEnter data: ";
 
                    cin>>data;
 
                    queue.enqueue(data);
 
            }else{
 
                coutQueue is Full"
 
            }
 
            break;
 
            case 2:
 
            if(!queue.isEmpty()){
 
                coutThe data dequeued is :"
 
            }else{
 
                coutQueue is Emtpy"
 
            }
 
                break;
 
            case 3:
 
                coutSize of Queue is "
 
                break;
 
            case 4:
 
                queue.display();
 
                break;
 
            case 5:
 
                exit(0);
 
                break;
 
        }
 
    }
 
    return 0;
 
}