Answer to Question #202724 in C++ for vhn

Question #202724

The queue is another data structure. A physical analogy for a queue is a line at a bank. When you go to the bank, customers go to the rear (end) of the line and customers who are serviced come out of the line from the front of the line. 

The main property of a queue is that objects go on the rear and come off of the front of the queue. 

• Make-Queue 

Create a new, empty queue object. 

• Empty 

Reports whether queue is empty or not. 

• Enter(or Insert) 

Places an object at the rear of the queue 

• Delete (or Remove) 

Removes an object from the front of the queue and produces that object. Write a program to create a queue class and do queue operations with exception handling. 


1
Expert's answer
2021-06-04T06:04:06-0400
#include <iostream>
#include <cstdlib>
using namespace std;
#define SIZE 7
 
class Queue
{
private:
    int *array1;
    int front;
    int rear;
    int capacity;
    int c;  
 
public:
    Queue(int size){
        array1 = new int[size];
        capacity = size;
        front = 0;
        rear = -1;
        c = 0;
    }
    int size() {
        return c;
    }
    bool isEmpty(){
       return (size() == 0); 
    }
    bool isFull() {
        return (size() == capacity);
    }
    void dequeue()
    {
        if (isEmpty())
        {
            cout << "Underflow";
        }
        else{
            cout<< array1[front]<<" is removed" << endl;
            front = (front + 1) % capacity;
            c--;
        }
    }
    void enqueue(int n)
    {
        if (isFull())
        {
            cout << "Overflow";
        }
        else{
            cout << n<<" is inserted" << endl;
            rear = (rear + 1) % capacity;
            array1[rear] = n;
            c++;
        }
    }
 
};
int main()
{
    Queue q(6);
 
    q.enqueue(23);
    q.enqueue(42);
    q.enqueue(34);
    q.dequeue();
    q.dequeue();
    q.enqueue(49);
    q.isEmpty();
    q.dequeue();
 
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog