954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

queues in data structures

template < class Object> 

void fix(queueType < Object> &q, int n) 
{ 

-if (n>=1) 

-{ 

---Object temp = q.front(); 
---q.deleteQueue(); 
---fix(q,n-1); 
---q.addQueue(temp); 
-} 

}


(a)what will the final state of queue q1 be after the statement fix(q1,2); is exexuted, if q1 has the values (1,2,3,4,5}

my answer: {2,3,4,5,1} because q.addQueue adds an element at the back
step 1: if (2>1) true
step 2: element {1} added to temp
step 3: q.deleteQueue(); deletes element{1}
step 4: fix(q1, 2-1)
step 5: q.addQueue(temp); element{1} is added at the back of the queue

b) what does function fix() do?

It restores the deleted element at the end of the queue

c) A precondition for fix() is: The queue exists and it is not empty

What do u think? Are my answers correct?

annitaz
Light Poster
28 posts since Jan 2010
Reputation Points: 5
Solved Threads: 0
 

Use CODE Tags when posting code.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>>Are my answers correct?

Not quite. Without giving anything away..

a)
Think a bit more about what happens at "step 4: fix(q1, 2-1)". This is a recursive call (a function calling itself again, usually with different parameters, in this case, n-1 instead of n).

b)
Already, with your current answer to (a), your answer to (b) is incorrect. Once you have worked out the correct answer to (a), this should be pretty easy to answer.

c)
A precondition for fix() is: The queue exists, it is not empty, and... it has at least (blank) elements.

mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

does it put element 1 back to its original place? 12345

annitaz
Light Poster
28 posts since Jan 2010
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: