Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Stack {
- // Variables
- public ArrayList <Double> stack;
- // Constructor
- Stack(){
- stack = new ArrayList <Double> ();
- }
- // Methods
- // Push
- public void push(double val) {
- stack.add(val);
- }
- // Pop
- public double pop() {
- if(stack.size() == 0) {
- System.out.println("Error. It is not permitted to pop element from a stack with size = 0");
- return -1000;
- }
- double value = stack.remove(stack.size()-1);
- return value;
- }
- // Max
- public double max() {
- if(stack.size() == 0) {
- System.out.println("Error. I cannot find man element in a stack with size = 0");
- return -1000;
- }
- double MAX = stack.get(0);
- for(int i=1; i<stack.size(); i++) {
- if(stack.get(i) > MAX) {
- MAX = stack.get(i);
- }
- }
- return MAX;
- }
- // Show
- public void show() {
- System.out.println("******** STACK ********");
- for(int i=0; i<stack.size(); i++) {
- System.out.println(stack.get(i));
- }
- System.out.println();
- }
- // MAIN FUNCTION
- public static void main(String args[]) {
- Stack STACK = new Stack();
- STACK.push(1);
- STACK.push(2);
- STACK.push(3);
- STACK.push(4);
- double popped = STACK.pop();
- System.out.println("Popped = " + popped);
- STACK.show();
- STACK.push(5);
- STACK.push(8);
- STACK.push(7);
- STACK.push(6);
- double MAX = STACK.max();
- System.out.println("Max = " + MAX);
- System.out.println();
- STACK.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement