Question
#include using namespace std; template class ArrayQueue { enum { DEF_CAPACITY = 100 }; // default queue capacity public: ArrayQueue(int cap = DEF_CAPACITY); // constructor
#include | |
using namespace std; | |
template | |
class ArrayQueue { | |
enum { DEF_CAPACITY = 100 }; // default queue capacity | |
public: | |
ArrayQueue(int cap = DEF_CAPACITY); // constructor from capacity | |
int size() const; // number of items in the stack | |
bool empty() const; // is the stack empty? | |
const E& front() const; // get the top element | |
void enqueue(const E& e); // add to back of queue | |
void dequeue(); // remove from front of queue | |
void printAll(); // print all elements on stack to cout | |
private: // member data | |
E* Q; // array of queue elements | |
int capacity; // queue capacity | |
int f; // index of the front of the queue | |
int r; // index of the rear of the queue | |
int n; // number of elements | |
}; | |
template | |
: Q(new E[cap]), capacity(cap), f(0), r(0), n(0) { } // constructor from capacity | |
template | |
{ | |
return n; | |
} // number of items in the queue | |
template | |
{ | |
return (n == 0); | |
} // is the stack empty? | |
template | |
const E& ArrayQueue | |
if (empty()) throw length_error("front of empty queue"); | |
return Q[f]; | |
} | |
template | |
void ArrayQueue | |
if (size() == capacity) throw length_error("enqueue to full queue"); | |
Q[r] = e; | |
r = (r + 1) % capacity; | |
n++; | |
} | |
template | |
void ArrayQueue | |
if (empty()) throw length_error("enqueue from empty queue"); | |
f = (f + 1) % capacity; | |
n--; | |
} | |
// print all elements on queue | |
template | |
void ArrayQueue | |
if (empty()) throw length_error("Empty queue"); | |
cout | |
int front = f; | |
for (int i = 0; i | |
cout | |
front = (front + 1) % capacity; | |
} | |
cout |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started