Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the definition of the function moveNthFront that takes as a parameter a po

ID: 3597997 • Letter: W

Question

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.

here's the code

#include <iostream>

#include <cassert>

using namespace std;

template <class Type>

class queueADT

{

public:

virtual bool isEmptyQueue() const = 0;

virtual bool isFullQueue() const = 0;

virtual void initializeQueue() = 0;

virtual Type front() const = 0; /

virtual Type back() const = 0;

virtual void addQueue(const Type& queueElement) = 0;

virtual void deleteQueue() = 0;

};

template <class Type>

class queueType : public queueADT<Type>

{

private:

int maxQueueSize;

int count;

int queueFront;

int queueRear;

Type *list;

  

public:

queueType(int queueSize = 100)

{

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;

  

queueFront = 0;

queueRear = maxQueueSize - 1;

count = 0;

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

}

  

queueType(const queueType<Type>& otherQueue) //Copy constructor

{

maxQueueSize = otherQueue.maxQueueSize;

queueFront = otherQueue.queueFront;

queueRear = otherQueue.queueRear;

count = otherQueue.count;

  

list = new Type[maxQueueSize];

  

  

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

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

}

  

  

~queueType()

{

delete[] list;

}

  

const queueType<Type>& operator=(const queueType<Type>& 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];

  

if (count != 0)

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

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

}

  

return *this;

}

  

  

bool isEmptyQueue() const

{

return (count == 0);

}

  

  

bool isFullQueue() const

{

return (count == maxQueueSize);

}

  

void initializeQueue()

{

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;

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

}

else

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

}

};

void testCopyConstructor(queueType<int> otherQueue);

int main()

{

queueType<int> 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<int> otherQueue)

{

if (!otherQueue.isEmptyQueue())

{

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

cout << "Front element of Other Queue : "

<< otherQueue.front() << endl;

otherQueue.deleteQueue();

}

}

Explanation / Answer

Here is your updated code with implemented function moveNthFront() in queueType class and the function is being called from main after queue2 is copied from queue1.

Here is your sample output. Change the value of n in main for different test cases.

/******************************/

Other Queue is not empty
Front element of Other Queue : 5
queue1: 5 9 16 4 2
Moving 4th element to Front
queue2: 4 5 9 16 2


/***************************/

/*
* Queue.cpp
*
* Created on: 22-Oct-2017
*      Author:
*/
#include <iostream>

#include <cassert>

using namespace std;

template <class Type>

class queueADT
{

public:

    virtual bool isEmptyQueue() const = 0;

    virtual bool isFullQueue() const = 0;

    virtual void initializeQueue() = 0;

    virtual Type front() const = 0;

    virtual Type back() const = 0;

    virtual void addQueue(const Type& queueElement) = 0;

    virtual void deleteQueue() = 0;

};

template <class Type>

class queueType : public queueADT<Type>

{

private:

    int maxQueueSize;

    int count;

    int queueFront;

    int queueRear;

    Type *list;

public:

    queueType(int queueSize = 100)

    {

        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;

        queueFront = 0;

        queueRear = maxQueueSize - 1;

        count = 0;

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

    }

    queueType(const queueType<Type>& otherQueue) //Copy constructor

    {

        maxQueueSize = otherQueue.maxQueueSize;

        queueFront = otherQueue.queueFront;

        queueRear = otherQueue.queueRear;

        count = otherQueue.count;

        list = new Type[maxQueueSize];

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

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

    }

    ~queueType()

    {

        delete[] list;

    }


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


    {

        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];

            if (count != 0)

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

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

        }

        return *this;

    }

    bool isEmptyQueue() const

    {

        return (count == 0);

    }

    bool isFullQueue() const

    {

        return (count == maxQueueSize);

    }

    void initializeQueue()

    {

        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;

            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

        }

        else

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

    }
    void moveNthFront(int n)
    {
       if(n>0 && n<=count)
       {
           Type tmp=list[n];
           for(int i=n;i>0;i--)
           {
               list[i]=list[i-1];
           }
           list[1]=tmp;
       }
       else
           cout<<"Element not found in queue"<<endl;
    }

};

void testCopyConstructor(queueType<int> otherQueue);

int main()

{

    queueType<int> 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;

    int n=4;
    cout<<"Moving "<<n<<"th element to Front"<<endl;
    queue2.moveNthFront(n);

    cout << "queue2: ";

    while (!queue2.isEmptyQueue())

    {

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

        queue2.deleteQueue();

    }

    cout << endl;

    return 0;

}

void testCopyConstructor(queueType<int> otherQueue)

{

    if (!otherQueue.isEmptyQueue())

    {

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

        cout << "Front element of Other Queue : "

        << otherQueue.front() << endl;

        otherQueue.deleteQueue();

    }

}