2. Write the definition of the function moveNthFront() that takes as a parameter
ID: 3856874 • Letter: 2
Question
2. Write the definition of the function moveNthFront() that takes as a parameter a positive integer n. Throw and catch the exception if entered not integer or not positive number. Prompt again to enter positive integer. The function moves the n-th element of the queue to the front. The order of the remaining elements remains unchanged. if no such element exists, throw an exception with appropriate message and leave queue 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, that described in this chapter 17. Also write a program to test your function moveNthFront(). Classes stackType and queueType and Driver to test operations are not provided.
Explanation / Answer
Answer for given function to implement the moveNthFront() function to move given number n to front without changing.
void arrayQueueType::moveNthFront(int n)
{
int temp ;
int x=length;
if(n<0)
{
cout<<"Re-Enter Number :"<<endl;
cin>>n;
}
temp = queue[n-1];
for(int i=0 ; i<n-1 ; i++)
{queueRear = (queueRear + 1) % maxSize;
queue[queueRear] = queue[i];
length++;}
x=length-x;
for(int i=0 ; i<n ; i++)
{
queueFront = (queueFront + 1) % maxSize;
length--;
}
for(int i=0 ; i<x ; i++)
{queueFront = (queueFront - 1) % maxSize;
queue[queueFront] = queue[queueRear];
length++;
queueRear = (queueRear -1) % maxSize;
length--;
}
queueFront = (queueFront - 1) % maxSize;
queue[queueFront]=temp;
length++;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.