/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package stack1325; /** * * @author jcmt * Stack1325 mystack = new Stack1325(); * Stack1325 mystack2 = new Stack1325(200); */ public class Stack1325 { private int[] intStack; private int topOfStack; //top of stack starts at 10 private int botOfStack; //bottom of empty stack is 10 private int numOfStEl; //number of elephants in the stack private int maxStEl; // in the constructor set initial values for top, bot, etc. public Stack1325() { intStack = new int[10]; topOfStack = 10; botOfStack = 10; maxStEl = 10; } public Stack1325(int size) { if (size <0) size = 10; intStack = new int[size]; topOfStack = size; botOfStack = size; maxStEl = size; } // push, pop, isEmpty, isFull, numberOfEle, public boolean isEmptyStk() { boolean flag = false; if (numOfStEl == 0) { flag = true; } return flag; } public boolean isFullStk() { boolean flag = false; if (topOfStack == 0) { flag = true; } return flag; } public boolean push(int newTop) { boolean flag = true; if (isFullStk() || (!(isValid(newTop)))) { flag = false; } else { if (isEmptyStk()) { botOfStack--; } topOfStack--; intStack[topOfStack] = newTop; numOfStEl++; } return flag; // return true; } private boolean isValid(int input) { boolean flag = true; if (input < 0) //inputs must be 0 or positive flag = false; return flag; //return (input >= 0); } public int pop() { if (isEmptyStk()) return -1; //Assume the values in stack must be pos numOfStEl--; topOfStack++; if (isEmptyStk()) botOfStack = maxStEl; return intStack[topOfStack-1]; } public void stkInspector() { if (isEmptyStk()) { System.out.println("The stack is empty."); } else { for (int i=topOfStack; i < maxStEl; i++) { System.out.println(intStack[i]); } } } }