Reverse order of elements on a slack, Data Structure & Algorithms

Q. Reverse the order of the elements on a stack S

   (i) by using two additional stacks

(ii) by using one additional queue.

Ans:      Let S be the stack having n number of elements. Now we need to reverse the elements of S (i) using the two additional stack S1 and S2

while not empty (S)

{       C=pop(S);

push(S1,C);

}

While not empty(S1)

{     C=pop(S1);

push(S2,C);

}

While not empty(S3)

{     C=pop(S3);

push(S,C);

}

(ii) using one additional queue Q

while not empty (S)

{       C=pop(S);

enque(Q,C);

}

While not empty(Q)

{     C=deque(Q);

push(S,C);

}

Posted Date: 7/10/2012 3:11:04 AM | Location : United States







Related Discussions:- Reverse order of elements on a slack, Assignment Help, Ask Question on Reverse order of elements on a slack, Get Answer, Expert's Help, Reverse order of elements on a slack Discussions

Write discussion on Reverse order of elements on a slack
Your posts are moderated
Related Questions
Example 1:  Following are Simple sequence of statements Statement 1;  Statement 2; ... ... Statement k; The entire time can be found out through adding the times for

A driver takes shortest possible route to attain destination. The problem which we will discuss here is similar to this type of finding shortest route in any specific graph. The gr

Write the algorithm for Binary search. Also apply this algorithm on the following data. 22, 44, 11, 88, 33, 55, 77, 66

basic calculation for algorith.

how to do a merge sorting

In the array implementation of lists, elements are stored into continuous locations. In order to add an element into the list at the end, we can insert it without any problem. But,

Implementing abstract data types A course in data structures and algorithms is hence a course in implementing abstract data types. It may seem that we are paying a lot of atten

If a node in a binary tree is not containing left or right child or it is a leaf node then that absence of child node can be represented by the null pointers. The space engaged by

In the previous unit, we have discussed arrays. Arrays are data structures of fixed size. Insertion and deletion involves reshuffling of array elements. Thus, array manipulation

an electrical student designed a circuit in which the impedence in one part of a series circuit is 2+j8 ohms and the impedent is another part of the circuit is 4-j60 ohm mm program