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
Answered by
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.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.