In: Computer Science
Solve in C++ program.
Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
Array implementation of queue data structure
#include<bits/stdc++.h>
using namespace std;
#define MAX_SIZE 101
class queue_implementation
{
private:
int array[MAX_SIZE];
int front, rear;
public:
//constructor implemenatation to set intial value of front and rear
queue_implementation()
{
front = -1;
rear = -1;
}
//check if queue is empty
bool IsEmpty()
{
return (front == -1 && rear == -1);
}
//check if queue reach its maximum size
bool IsFull()
{
return (rear+1)%MAX_SIZE == front ? true : false;
}
// Inserts an element in queue at rear end
void insert_element (int x)
{
cout<<"Inseted element is : "<<x<<" \n";
if(IsFull())
{
cout<<"Error: Queue is Overflow \n";
return;
}
if (IsEmpty())
{
front = rear = 0;
}
else
{
rear = (rear+1)%MAX_SIZE;
}
array[rear] = x;
}
//dequue element at the queue implemenatation
void remove_element ()
{
cout<<" Element Removed after Dequeing : "<< "\n";
if(IsEmpty())
{
cout<<"Error: Queue is Underflow\n";
return;
}
else if(front == rear )
{
rear = front = -1;
}
else
{
front = (front+1)%MAX_SIZE;
}
}
// Returns element at front of queue.
int Front()
{
if(front == -1)
{
cout<<"Error: cannot return front from empty queue\n";
return -1;
}
return array[front];
}
//traverse from rear to front
void traverse()
{
// Finding number of elements in queue
int count = (rear+MAX_SIZE-front)%MAX_SIZE + 1;
cout<<"Queue : ";
for(int i = 0; i <count; i++)
{
int index = (front+i) % MAX_SIZE;
cout<<array[index]<<" ";
}
cout<<"\n\n";
}
};
int main()
{
queue_implementation queue;
cout<<"After Enqueueing : \n";
queue.insert_element (1);
queue.insert_element (2);
queue.insert_element (3);
queue.insert_element (4);
queue.insert_element (5);
queue.insert_element (6);
queue.insert_element (7);
queue.insert_element (8);
queue.insert_element (9);
queue.insert_element (10);
queue.traverse();
cout<<"After dequeing 2 elements : "<<endl;
queue.remove_element();
queue.remove_element();
queue.traverse();
}
output_screen