Sollicitatievraag bij Morgan Stanley

reverse a stack without "new" key word in java

Antwoord op sollicitatievraag

Anoniem

18 aug 2020

we could do it using recursion - let suppose - #####Function 1####### void reverseStack() { if(stack.size() > 0) { char item = stack.peek(); stack.pop(); reverse(); // to go deep in stack or end of the stack pushFromBottom(item); } } #####Function 2####### void pushFromBottom(char x) { if(stack.isEmpty()) { stack.push(x); } else { char item = stack.peek(); stack.pop(); pushFromBottom(x); //place top value at the end. stack.push(item); //push the other items } }