Please try to use a simplest way to finish this problem( In Java) Consider an al
ID: 3732936 • Letter: P
Question
Please try to use a simplest way to finish this problem( In Java)
Consider an algorithm that evaluates an arithmetic expression using “Postfix” notation using the “MyStack” class. You can use JAVA or pseudo-code to implement the following:
It may be the case that the expression you supply is invalid, so an attempt to calculate the answer would result in an error. The evaluation program should include a routine to check whether the expression is valid and indicate to the user, e.g., through an error code, if the expression is invalid.
For the MyStack class, we created methods like 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 a1 onto the top of the stack; and pop() that removes the top element of the stack, if any. To process any stack in a Main program, you may only use those routines to manipulate the stack.
Design a function called getBottomValue(MyStack S) that returns the value at the bottom of the stack.
Design a function called stackStack(MyStack S, MyStack T) that creates a new stack containing the values from S below the values from stack T, in the correct order. When the routine is done, the top element should be the top element from T. S and T should be restored to their original conditions.
Illustration:
2 3 3
5 7 7
4 1 1
6 Stack T 2
Stack S 5
4
6
Resulting
Stack
Explanation / Answer
Code with comments:
Copy the code and store it in Main.java file.
import java.util.*;
// class having basic functionality as mentioned in question
class MyStack {
private List<Integer> stackList;
//Constructor
public MyStack() {
stackList = new ArrayList<Integer>();
}
// pushes the integer into stack
public void push(Integer x) {
stackList.add(x);
}
// pops out the integer
public void pop() {
stackList.remove(stackList.size()-1);
}
// checks if the stack is empty.
public boolean emptyCheck() {
return stackList.isEmpty();
}
// returns the top element of stack.
public Integer top() {
return stackList.get(stackList.size()-1);
}
}
public class Main {
public static Integer getBottomValue(MyStack s) {
MyStack temp = new MyStack();
// A temporary stack is created by poping from stack s.
while(!s.emptyCheck()) {
temp.push(s.top());
s.pop();
}
// Stack temp is in reverse order
// thus the top most element in temp is the last element in stack s.
Integer bot = temp.top();
// Restoring the stack.
while(!temp.emptyCheck()) {
s.push(temp.top());
temp.pop();
}
// returning the topmost element.
return bot;
}
public static MyStack stackStack(MyStack S, MyStack T) {
MyStack temp = new MyStack(); // temporary stack
MyStack finalRet = new MyStack(); //final ans stack
//inserting elements in reverse order from stack S to temp
while(!S.emptyCheck()) {
temp.push(S.top());
S.pop();
}
// restoring the stack S and at same time putting elements in answer stack
while(!temp.emptyCheck()) {
finalRet.push(temp.top());
S.push(temp.top());
temp.pop();
}
//inserting elements in reverse order from stack T to temp
while(!T.emptyCheck()) {
temp.push(T.top());
T.pop();
}
// restoring the stack S and at same time putting elements in answer stack
while(!temp.emptyCheck()) {
finalRet.push(temp.top());
T.push(temp.top());
temp.pop();
}
return finalRet;
}
public static void main(String[] args) {
MyStack S = new MyStack();
S.push(6);
S.push(4);
S.push(5);
S.push(2);
// System.out.println(getBottomValue(S));
MyStack T = new MyStack();
T.push(1);
T.push(7);
T.push(3);
MyStack ms = stackStack(S, T);
while(!ms.emptyCheck()) {
System.out.println(ms.top());
ms.pop();
}
}
}
Otuput:
$ javac Main.java
$ java Main
3
7
1
2
5
4
6