Implement a Queue ADT using a circular arraywith 5 string elements.Create
a Queue object and try various operations below, in C++.
Queue myQueue;
myQueue.enqueue(“Computer 1”);
myQueue.enqueue(“Computer 2”);
myQueue.enqueue(“Computer 3”);
myQueue.dequeue();
myQueue.enqueue(“Computer 4”);
myQueue.enqueue(“Computer 5”);
string course = myQueue.front(); // computer should be Comp 2
size = myQueue.size();// size should be 4
// output course and size
#include <iostream> using namespace std; class Queue { string *arr; int queueFront; int queueRear; int capacity; int numElements; void grow(int newsize) { string *newArray = new string[newsize]; int count = 0; if (queueRear >= queueFront) { for (int i = queueFront; i <= queueRear; i++) { newArray[count++] = arr[i]; } } else { for (int i = queueFront; i < capacity; i++) { newArray[count++] = arr[i]; } for (int i = 0; i <= queueRear; i++) { newArray[count++] = arr[i]; } } if(count == 0) { queueFront = queueRear = -1; } else { queueFront = 0; queueRear = count - 1; } capacity = newsize; delete [] arr; arr = newArray; } public: Queue() { capacity = 100; arr = new string[capacity]; queueFront = -1; queueRear = -1; numElements = 0; } void enqueue(string value) { if ((queueFront == 0 && queueRear == capacity-1) || (queueRear == (queueFront-1)%(capacity-1))) { //Queue is Full grow(2 * capacity); enqueue(value); return; } else if(queueFront == -1) { // empty queue queueFront = queueRear = 0; arr[queueRear] = value; } else if(queueRear == capacity-1 && queueFront != 0) { queueRear = 0; arr[queueRear] = value; } else { queueRear += 1; arr[queueRear] = value; } numElements++; } string dequeue() { if(queueFront == -1) { return NULL; } string data = arr[queueFront]; if(queueFront == queueRear) { // single element queueFront = -1; queueRear = -1; } else if (queueFront == capacity-1) { queueFront = 0; } else { queueFront++; } numElements--; return data; } // Just returns the front element O(1) string front() { if(queueFront != -1) { return arr[queueFront]; } else { return NULL; } } int size() { return numElements; } }; int main() { Queue myQueue; myQueue.enqueue("Computer 1"); myQueue.enqueue("Computer 2"); myQueue.enqueue("Computer 3"); myQueue.dequeue(); myQueue.enqueue("Computer 4"); myQueue.enqueue("Computer 5"); string course = myQueue.front(); // computer should be Comp 2 int size = myQueue.size();// size should be 4 // output course and size cout << course << endl; cout << size << endl; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.