Asked by ahmad
Write a java method that will take two sorted stacks A and B (min on top), and return one
stack D that is sorted (min on top). You are allowed to use only the stack operations such as
pop, push, isEmpty and peek.
Example: suppose A={(top)1,4,7,9} and B={(top)2,3,6}, then the function will return a new
stack D= {(top) 1,2,3,4,6,7,9}
Note: No other data structure is allowed other than stacks.
stack D that is sorted (min on top). You are allowed to use only the stack operations such as
pop, push, isEmpty and peek.
Example: suppose A={(top)1,4,7,9} and B={(top)2,3,6}, then the function will return a new
stack D= {(top) 1,2,3,4,6,7,9}
Note: No other data structure is allowed other than stacks.
Answers
Steve
all you have to do is read down through each stack, always transferring the lesser element to temporary stack E. Then at the end, pop E onto D until empty.