Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   data sructures queues (http://www.go4expert.com/forums/data-sructures-queues-t25908/)

annitaz 26May2011 10:56

data sructures queues
 
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?


All times are GMT +5.5. The time now is 09:39.