Question: Design for array-based queue requirement: Write a function that the user selects queue i (0i9), and then inserts any number of numbers into queue i.
Design for array-based queue
requirement:
Write a function that the user selects queue i (0i9), and then inserts any number of numbers into queue i. Input "#" to indicate that the user does not select any queue. At the end of the program, the non-empty queue among the 10 queues is output in the order of queue number from the smallest to the largest.
For example
Input
0(queue number)
1 3 5 7 9 8
1(queue number)
2 4 6
9(queue number)
111 222 333
#
Output
1 3 5 7 9 8 2 4 6 111 222 333
complete the code below to fulfill the requirement
below is the code in this picture
#include
#include
#include
using namespace std;
#define defaultSize 10
void Assert(bool val, string s)
{
if (!val)
{ // Assertion failed -- close the program
cout
exit(-1);
}
}
// Abstract queue class
template
class Queue
{
private:
void operator=(const Queue &) {} // Protect assignment
Queue(const Queue &) {} // Protect copy constructor
public:
Queue() {} // Default
virtual ~Queue() {} // Base destructor
// Reinitialize the queue. The user is responsible for
// reclaiming the storage used by the queue elements.
virtual void clear() = 0;
// Place an element at the rear of the queue.
// it: The element being enqueued.
virtual void enqueue(const E &) = 0;
// Remove and return element at the front of the queue.
// Return: The element at the front of the queue.
virtual E dequeue() = 0;
// Return: A copy of the front element.
virtual const E &frontValue() const = 0;
// Return: The number of elements in the queue.
virtual int length() const = 0;
};
// Array-based queue implementation
template
class AQueue : public Queue
{
private:
int maxSize; // Maximum size of queue
int front; // Index of front element
int rear; // Index of rear element
E *listArray; // Array holding queue elements
public:
AQueue(int size = defaultSize)
{ // Constructor
// Make list array one position larger for empty slot
maxSize = size + 1;
rear = 0;
front = 1;
listArray = new E[maxSize];
}
~AQueue() { delete[] listArray; } // Destructor
void clear()
{
rear = 0;
front = 1;
} // Reinitialize
void enqueue(const E &it)
{ // Put "it" in queue
Assert(((rear + 2) % maxSize) != front, "Queue is full");
rear = (rear + 1) % maxSize; // Circular increment
listArray[rear] = it;
}
E dequeue()
{ // Take element out
Assert(length() != 0, "Queue is empty");
E it = listArray[front];
front = (front + 1) % maxSize; // Circular increment
return it;
}
const E &frontValue() const
{ // Get front value
Assert(length() != 0, "Queue is empty");
return listArray[front];
}
virtual int length() const // Return length
{
return ((rear + maxSize) - front + 1) % maxSize;
}
bool empty() {
return maxSize == 0;
}
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
