2) For our MyStack class we created methods emptyCheck() which returns True if t
ID: 3731740 • Letter: 2
Question
2) For our MyStack class we created methods emptyCheck() which returns True if the stack becomes empty; top), that returns the value of the top element in the stack; push(a1) that places the value of al onto the top of the stack; and popi) that removes the top element of the stack, if any. To process any stack, you may only use those routines and some primitive data variables to temporarily store a value or two. If necessary, you can temporarily create a second stack to store values. a) Design a method in MyStack called getBottomValue() that returns the values at the bottom of the stack. Design a method in MyStack called stackStack(MyStack that pushes the values from stack T, in the correct order. When the routine is done, the top element in the ("this") stack should be the top element from T. T should also be restored to its original condition. b) Illustration Stack T 6 The ('this") Stack Resulting "this" StackExplanation / Answer
public void pushAtBottom(int x){
//
if(isEmpty())
push(x);
else{
int a = peek();
pop();
pushAtBottom(x);
push(a);
}
}
public void reverse()
{
if(top>=0)
{
// poping all the values until stack is empty
// after that we are calling the pushAtBottom function calling
// 2 3 4 5 popped and calling pushAtBottom(2),pushAtBottom(3)pushAtBottom(4)pushAtBottom(5)
int x = peek();
pop();
reverse();
pushAtBottom(x);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.