package example1; // Note: This class implements a stack of Strings only. public class MyStack { // The maximum possible size of the stack. public static final int MaxSize = 100; // The array index of the last active element // in the stack. A value of -1 means the stack // is currently empty. private static int top = -1; // The stack elements, stored in an array. private static String[] store = new String[MaxSize]; // Add a new element at the top of the stack // (if there’s space left). public static void push(String newVal) { if (top < MaxSize - 1) { top++; store[top] = newVal; } else { System.out.println("Sorry, stack full."); } } // Remove the top element of the stack and return // it; check that the stack has elements in it! public static String pop() { String ans = ""; if (top >= 0) { ans = store[top]; top--; } else { System.out.println("Sorry, stack empty."); } return ans; } // Return the number of elements that are // currently in the stack. public static int size() { return top + 1; } }