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
what are registers? why we need register? Definition? Types? What registers can do for us?

Double ended queues are implemented along doubly linked lists. A doubly link list can traverse in both of the directions as it contain two pointers namely left pointers and righ

what is hashing? what are diffrent method of hashing?

A Binary Search Tree is binary tree which is either empty or a node having a key value, left child & right child. By analyzing the above definition, we notice that BST comes int

First - Fit Method: -    The free list is traversed sequentially to search the 1st free block whose size is larger than or equal to the amount requested. Once the block is found it

Program Insertion of a node into any Circular Linked List Figure depicts a Circular linked list from which an element was deleted. ALGORITHM (Deletion of an element from a

Sort the following array of elements using quick sort: 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8.

Operations on B-Trees Given are various operations which can be performed on B-Trees: Search Create Insert B-Tree does effort to minimize disk access and t

Write an algorithm by using pseudocode which: Inputs top speeds of 5000 cars Outputs fastest speed and the slowest speed Outputs average speed of all the 5000 cars

Complexity classes All decision problems fall in sets of comparable complexity, called as complexity classes. The complexity class P is the set of decision problems which ca