Question: Just need to show the two functions for the answer: You only need to write and show the C++ codes to implement the below two

Just need to show the two functions for the answer:

You only need to write and show the C++ codes to implement the below two functions:

a. void resize(int N);

This function resizes the current queue array capacity to N.

b. void enqueue(const E& e);

This function appends a new element to the back of the queue. If the queue is full then it calls the resize function to double the current capacity and then en-queue the new element to the new capacity queue.

Example of an Extendable Array Queue Class declaration:

template

class ExtArrayQueue {

enum { DEF_CAPACITY = 5 }; // default queue capacity

public:

ExtArrayQueue(int cap = DEF_CAPACITY); // constructor from capacity

int size() const; // number of items in the queue

bool empty() const; // is the queue empty?

const E& front() const; // get the front element

void enqueue(const E& e); // add to back of queue

void dequeue(); // remove from front of queue

void printAll(); // print all elements in the queue

Private:

void resize(int N); // resize the array to size N

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

};

#pragma once

#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 ArrayQueue::ArrayQueue(int cap) : Q(new E[cap]), capacity(cap), f(0), r(0), n(0) { } // constructor from capacity

template int ArrayQueue::size() const { return n; } // number of items in the queue

template bool ArrayQueue::empty() const { return (n == 0); } // is the stack empty?

template // return element at front of queue const E& ArrayQueue::front() const { if (empty()) throw length_error("front of empty queue"); return Q[f]; }

template // insert element to back of queue void ArrayQueue::enqueue(const E& e) { if (size() == capacity) throw length_error("enqueue to full queue"); Q[r] = e; r = (r + 1) % capacity; n++; }

template // remove element at front of queue void ArrayQueue::dequeue() { if (empty()) throw length_error("enqueue from empty queue"); f = (f + 1) % capacity; n--; }

// print all elements on queue template void ArrayQueue::printAll() { if (empty()) throw length_error("Empty queue"); cout << "Elements in queue: "; int front = f; for (int i = 0; i < n; i++) { cout << "{" << Q[front] << "} "; front = (front + 1) % capacity; } cout << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!