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: 3695292 • 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.

I'm trying to use insert by position function to move the nth element to the front of the queue. it's working for all postions except for the 0 psoition. PLEASE HELP.

#ifndef queueType_H
#define queueType_H
#include<iostream>

class queueType
{
private:
   class Queue
   {
       friend class queueType;
       int value;
       Queue *next;
       Queue(int value1, Queue *next1 = NULL)
       {
           value = value1;
           next = next1;
       }
   };
   // These track the front and rear of the queue
   Queue *front;
   Queue *rear;
   Queue *head;
  

public:
   // Constructor and Destructor
   queueType();
   ~queueType();

   // Member functions
   void enqueue(int);
   void insert(int, int);
   void dequeue(int &);
   void clear();
   int search(int);
   bool isEmpty() const;
   void remove(int);
   void moveNthFront(int);
};
#endif

#include"queueType.h"
#include<iostream>

using namespace std;

queueType::queueType() {
   front = NULL;
   rear = NULL;
}

queueType::~queueType() {
   clear();
}

void queueType::enqueue(int num)
{
   if (isEmpty())
   {
       front = new Queue(num);
       rear = front;
   }
   else
   {
       rear->next = new Queue(num);
       rear = rear->next;
   }
}
void queueType::dequeue(int &num)
{
   Queue *temp;
   if (isEmpty())
   {
       cout << "The queue is empty. ";
       exit(1);
   }
   else
   {
       num = front->value;
       temp = front;
       front = front->next;
       delete temp;
   }
}

bool queueType::isEmpty() const
{
   if (front == NULL)

       return true;
   else
       return false;
}

int queueType::search(int x)
{
   if (front == NULL)
       return -1;
   else
   {
       int count = 0;
       Queue *aptr = front;
       while (aptr != NULL)
       {
           if (aptr->value == x)
               return count;
           aptr = aptr->next;
           count++;
       }
       return -1;
   }
}

void queueType::clear()
{
   int value; // Dummy variable for dequeue

   while (!isEmpty())
       dequeue(value);
}
void queueType::remove(int pos)
{
   if (front == NULL)
       return;
   else if (pos == 0)
       front = front->next;
   else
   {
       int count = 0;
       Queue *now = front, *past;
       while (now != NULL && count != pos)
       {
           past = now;
           now = now->next;
           count++;
       }
       if (now)
       {
           past->next = now->next;
           delete now;
       }
   }
}
void queueType::insert(int x, int pos )
{
   Queue *now, *past;
   if (front == NULL)
       front = new Queue(x);
   else
   {
       now = front;
  
       int count = 0;
       while (now != NULL && count != pos)
       {
           past = now;
           now = now->next;
           count++;
       }
       past->next = new Queue(x, now);
   }
}

#include"queueType.h"
#include<iostream>
using namespace std;

int main() {

   queueType intqueue;

   int input, temp, x = 0;

   for (int i = 0; i < 5; i++) {
       intqueue.enqueue(i*i);
   }
   cout << "The values in the queue were: ";
   while (!intqueue.isEmpty())
   {
       int value;
       intqueue.dequeue(value);
       cout << value << " ";
   }

   for (int i = 0; i < 5; i++) {
       intqueue.enqueue(i*i);
   }
   cout << " Enter the value to find:" << endl;
   cin >> input;
   intqueue.search(input);
   temp = intqueue.search(input);
   intqueue.remove(temp);
   intqueue.insert(input, 0);

   cout << " The values after change was made: ";
   while (!intqueue.isEmpty())
   {
       int value;
       intqueue.dequeue(value);
       cout << value << " ";
   }
   return 0;
}

Explanation / Answer

void moveNthFront(queueType &queue,int n)
{
//tempororay queue declared
queueType tempQueue;
//until processed all values in queue
while(!queue.isEmpty())
{
//checking value found is n
if(queue.retrieve()!=n){
tempQueue.append(queue.serve());
}
else {//getting the nth index value
n=queue.serve(); //serve gives the front next element
}
}
//making first slot empty
queue.frontzero();
//appending nth index value at head position
queue.append(n);
//then adding remaining elements next to front
while(!tempQueue.isEmpty())
{
queue.append(tempQueue.serve());
}
queue.print();
}