Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

14. Write the definition of the function moveNthFront that takes as a parameter a positive integer, n. The function moves the nth element of the

14. Write the definition of the function moveNthFront that takes as a parameter a positive integer, n. The function moves the nth element of the queue to the front. The order of the remaining elements remains unchanged. For example, suppose: queue = {5, 11, 34, 67, 43, 55} and n = 3. After a call to the function moveNthFront: queue = {34, 5, 11, 67, 43, 55}. Add this function to the class queueType. Also, write a program to test your method.

I am currentlt displaying

queue: 5 9 16 4 2

queue: 5 9 16 4 2

#include

#include

using namespace std;

template

class queueADT

{

public:

virtual bool isEmptyQueue() const = 0;

virtual bool isFullQueue() const = 0;

virtual void initializeQueue() = 0;

virtual Type front() const = 0; //Function to return the first element of the queue.

virtual Type back() const = 0; //Function to return the last element of the queue.

virtual void addQueue(const Type& queueElement) = 0; //Function to add queueElement to the queue.

virtual void deleteQueue() = 0; //Function to remove the first element of the queue.

};

template

class queueType : public queueADT

{

private:

int maxQueueSize; //variable to store the maximum queue size

int count; //variable to store the number of elements in the queue

int queueFront; //variable to point to the first element of the queue

int queueRear; //variable to point to the last element of the queue

Type *list; //pointer to the array that holds the queue elements

public:

queueType(int queueSize = 100) //Constructor

{

if (queueSize <= 0)

{

cerr << "Size of the array to hold the queue must "

<< "be positive." << endl;

cerr << "Creating an array of size 100." << endl;

maxQueueSize = 100;

}

else

maxQueueSize = queueSize; //set maxQueueSize to queueSize

queueFront = 0; //initialize queueFront

queueRear = maxQueueSize - 1; //initialize queueRear

count = 0;

list = new Type[maxQueueSize]; //create the array to hold the queue elements

}

queueType(const queueType& otherQueue) //Copy constructor

{

maxQueueSize = otherQueue.maxQueueSize;

queueFront = otherQueue.queueFront;

queueRear = otherQueue.queueRear;

count = otherQueue.count;

list = new Type[maxQueueSize];

//copy other queue in this queue

for (int j = queueFront; j <= queueRear; j = (j + 1) % maxQueueSize)

list[j] = otherQueue.list[j];

} //end copy constructor

~queueType() //Destructor

{

delete[] list;

}

const queueType& operator=(const queueType& otherQueue) //Overload the assignment operator.

{

int j;

if (this != &otherQueue) //avoid self-copy

{

maxQueueSize = otherQueue.maxQueueSize;

queueFront = otherQueue.queueFront;

queueRear = otherQueue.queueRear;

count = otherQueue.count;

delete[] list;

list = new Type[maxQueueSize];

//copy other queue in this queue

if (count != 0)

for (j = queueFront; j <= queueRear; j = (j + 1) % maxQueueSize)

list[j] = otherQueue.list[j];

} //end if

return *this;

}

bool isEmptyQueue() const //Function to determine whether the queue is empty.

{

return (count == 0);

} //end isEmptyQueue

bool isFullQueue() const //Function to determine whether the queue is full.

{

return (count == maxQueueSize);

} //end isFullQueue

void initializeQueue() //Function to initialize the queue to an empty state.

{

queueFront = 0;

queueRear = maxQueueSize - 1;

count = 0;

}

Type front() const //Function to return the first element of the queue.

{

assert(!isEmptyQueue());

return list[queueFront];

}

Type back() const //Function to return the last element of the queue.

{

assert(!isEmptyQueue());

return list[queueRear];

} //end back

void addQueue(const Type& newElement) //Function to add queueElement to the queue.

{

if (!isFullQueue())

{

queueRear = (queueRear + 1) % maxQueueSize; //use mod

//operator to advance queueRear

//because the array is circular

count++;

list[queueRear] = newElement;

}

else

cerr << "Cannot add to a full queue." << endl;

}

void deleteQueue() //Function to remove the first element of the queue.

{

if (!isEmptyQueue())

{

count--;

queueFront = (queueFront + 1) % maxQueueSize; //use the

//mod operator to advance queueFront

//because the array is circular

}

else

cerr << "Cannot remove from an empty queue." << endl;

}

};

void testCopyConstructor(queueType otherQueue);

int main()

{

queueType queue1, queue2;

int x, y;

x = 4;

y = 5;

queue1.addQueue(x);

queue1.addQueue(y);

x = queue1.front();

queue1.deleteQueue();

queue1.addQueue(x + 5);

queue1.addQueue(16);

queue1.addQueue(x);

queue1.addQueue(y - 3);

testCopyConstructor(queue1);

queue2 = queue1;

cout << "queue1: ";

while (!queue1.isEmptyQueue())

{

cout << queue1.front() << " ";

queue1.deleteQueue();

}

cout << endl;

cout << "queue2: ";

while (!queue2.isEmptyQueue())

{

cout << queue2.front() << " ";

queue2.deleteQueue();

}

cout << endl;

return 0;

}

void testCopyConstructor(queueType otherQueue)

{

if (!otherQueue.isEmptyQueue())

{

cout << "Other Queue is not empty" << endl;

cout << "Front element of Other Queue : "

<< otherQueue.front() << endl;

otherQueue.deleteQueue();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

1.what is the significance of Taxonomy ?

Answered: 1 week ago

Question

What are the advantages and disadvantages of leasing ?

Answered: 1 week ago

Question

Name is needed for identifying organisms ?

Answered: 1 week ago