The following question involves the class QueueInterface which contains all the
ID: 3850913 • Letter: T
Question
The following question involves the class QueueInterface which contains all the functions required for a queue. QueueInterface contains elements of type int. i. Write the definition for QueueInterface. ii. Define a class called VectorQueue which inherits from QueueInterface. VectorQueue uses a vector for its queue. iii. Implement a new function of VectorQueue that returns an element of the queue without removing it from the queue. You will need to decide which element to return. Explain your choice of element.Explanation / Answer
All the three parts are combined together in the code below
PROGRAM CODE:
#include <iostream>
#include <vector>
using namespace std;
//i) this is the definition for QueueInterface
class QueueInterface
{
public:
virtual void enqueue(int data)=0;
virtual int dequeue()=0;
virtual int peek()=0;
virtual void clear()=0;
virtual int size()=0;
virtual bool isEmpty()=0;
virtual bool isFull()=0;
virtual void display()=0;
};
//ii) Implementation of VectorQueue class
class VectorQueue: public QueueInterface
{
private:
vector<int> contents;
int currentSize;
int capacity;
public:
//constructor for the queue class
VectorQueue(int c)
{
capacity = c;
clear();
}
//adds an element to the end of the queue
void enqueue(int data)
{
contents[currentSize++] = data;
}
//removes element from the end of the queue
int dequeue()
{
int data = contents[0];
contents.erase(contents.begin());
currentSize--;
return data;
}
//iii) peek method returns the front element without removing it
int peek()
{
return contents[0];
}
//cleard the items in the queue
void clear()
{
currentSize = 0;
contents.clear();
contents.resize(capacity);
}
//returns the siz eof the queue
int size()
{
return currentSize;
}
//returns true if queue is empty
bool isEmpty()
{
return currentSize == 0;
}
//returns true if queue is full
bool isFull()
{
return currentSize == capacity;
}
//displayes the queue to the screen
void display()
{
for(int i=0; i<currentSize; i++)
{
cout<<contents[i]<<" ";
}
cout<<endl;
}
};
int main() {
VectorQueue queue(5);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.display();
return 0;
}
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.