Use the following UML diagram and class description to create your own static qu
ID: 3725704 • Letter: U
Question
Use the following UML diagram and class description to create your own static queue that stores integers. Accessors should be marked cons queue: int front: int rear: int max: int MyQueuels: int: MyQueue0 enqueue: int): int dequeueo: int toplt int&):int queue-a pointer that stores the memory address for a dynamically allocated array of integers. front-stores the index of the element that stores the value at the front of the queue. rear-stores the index of the element where the next value will be added to the queue. max-stores the the size of the array nstructor-stores it's argument in max, dynamically allocates an array of max elements, and initializes front and rear to O. destructor-frees all memory used by the object enqueue-adds it's argument to the end of the queue. Returns O if successful, -1 otherwise. Will fail if the queue is full. dequeue-removes the value at the front of the queue. Returns O if successful,-1 otherwise. Will fail if the queue is empty. top-assigns the front value to it's reference parameter. Returns 0 successful, -1 otherwise. Will fail if the queue is empty · Place your entire class in it's own header file named MyQueue.h. Remember to define your methods. Remember to include preprocessor header gaurd.er to defineyour HintsWrite your own program to test your queue. Test it by enqueueinga the right order.Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
class MyQueue{
private:
int* item;
int rear;
int front;
public:
int MAX_SIZE;
MyQueue(int size);
~MyQueue();
void enqueue(int);
int dequeue();
int top();
void display();
bool isEmpty();
bool isFull();
};
MyQueue::MyQueue(int size){
this->MAX_SIZE = size;
this->item = new int[size];
rear = -1;
front = 0;
}
MyQueue::~MyQueue(){
delete []item;
}
void MyQueue::enqueue(int data){
item[++rear] = data;
}
int MyQueue::dequeue(){
return item[front++];
}
void MyQueue::display(){
if(!this->isEmpty()){
for(int i=front; i<=rear; i++)
cout<<item[i]<<endl;
}else{
cout<<"Queue Underflow"<<endl;
}
}
int MyQueue::top(){
return (rear - front + 1);
}
bool MyQueue::isEmpty(){
if(front>rear){
return true;
}else{
return false;
}
}
bool MyQueue::isFull(){
if(this->top()>=MAX_SIZE){
return true;
}else{
return false;
}
}
int main(){
MyQueue queue(50);
int choice, data;
queue.enqueue(1);
queue.enqueue(3);
cout<<queue.dequeue()<<" ";
cout<<queue.dequeue()<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.