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

Assignment Help:

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);

}


Related Discussions:- Reverse order of elements on a slack

Implementation of stack, Implementation of Stack :- Stacks can be execu...

Implementation of Stack :- Stacks can be executed in the 2 ways: a)  Arrays b)  Linked List

Deletion of a node from an avl tree, For AVL trees the deletion algorithm i...

For AVL trees the deletion algorithm is a little more complicated as there are various extra steps involved in the deletion of node. If the node is not a leaf node, then it contain

What are the dynamic arrays, What are the Dynamic arrays Dynamic arrays...

What are the Dynamic arrays Dynamic arrays are convenient for programmers since they can never be too small-whenever more space is needed in a dynamic array, it can simply be e

A difference between linear and non linear, state difference between linear...

state difference between linear and non linear data structure. give one example scenario of each

Dqueue, algorithm of output restricted queue.

algorithm of output restricted queue.

Assignment, How do I submit a three page assignment

How do I submit a three page assignment

List various problem solving techniques, List various problem solving techn...

List various problem solving techniques. There are two techniques:- 1.  Top down 2.  Bottom- up

Tree, application of threaded binary treee

application of threaded binary treee

What is class invariants assertion, What is Class invariants assertion ...

What is Class invariants assertion A class invariant is an assertion which should be true of any class instance before and after calls of its exported operations. Generally

Train reorganising, A freight train from Melbourne is approaching Sydney, c...

A freight train from Melbourne is approaching Sydney, carrying n cars of cargos. The cargos are to be delivered to n different cities in the metropolitan area of Sydney - one car f

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd