Write a function moves the nth element of the queue to the front.

I've been struggling with C++ since day one and have been barely surviving. The assignment I'm working on now is to write a 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. I don't understand how I'm going to move Nth element. I also have to add this function to the class queueType and write a program to test my method. I don't have very much so far and I don't even know if it's right. Can someone point me in the right direction? I'm using MS Visual C++ 2010. Thank you.

template <class Type>
void queuetype<Type>::moveNthFront()

void moveNthFront(int n)
{


}
Last edited on
closed account (SECMoG1T)
Hi you can use another queue to assist you
Because a queue is a FIFO structure we will hold the nth value in another variable then store the rest of the variables in a temporal queue while emptying the original queue and then load them into the original
Queue again with the nth element leading


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 template <class Type>
 void queuetype<Type>::moveNthFront( int n)
  {
    queue <Type> temp; 
    Type val; // will hold the variable to be moved
    /*myq;*/// I dint see where your queue is coz you dint mention it as a param, so I assume it is a class 
              ///member say myq.
    int count=1;

     while (!myq.empty ())/// lets empty the whole queue
       {
           if (count==n)/// hold nth element in a temporal variable
              {val=myq.front (); myq.pop ();}
           else
             {
               temp.push (myq.front ()); 
               myq.pop ();
               count++;
             }
        }///original queue is empty now
  
       /// let's make our variable the first element of the myq
         myq.push (val);
       
      while (! temp.empty ())/// load the rest into myq
        {
          myq.push (temp.front ());
          temp.pop ();
        }
 
   }


Build on that
Last edited on
Topic archived. No new replies allowed.