/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package graph; /** * * @author suchenek */ public class STACK { private int count; private int capacity; private int capacityIncrement; private int[] itemArray; /** Creates a new instance of PriorityQueue */ public STACK() { count=0; capacity=10; capacityIncrement=2; itemArray=new int[capacity]; } public void push(int newItem) { if(count==capacity) //no more space, "resize" the array up { capacity*=capacityIncrement; int[] tempArray = new int[capacity]; for (int i = 0; i < count; i++) { tempArray[i] = itemArray[i]; } itemArray = tempArray; } itemArray[count++] = newItem; } public int pop() { int itemToReturn; if (count==0) return -99999; itemToReturn=itemArray[--count]; if ((count < capacity / capacityIncrement) && (10 <= capacity / capacityIncrement)) //space wasted, "resize" the array down { capacity/=capacityIncrement; int[] tempArray = new int[capacity]; for (int i = 0; i < count; i++) { tempArray[i] = itemArray[i]; } itemArray = tempArray; } return itemToReturn; } public int size() { return count; } public boolean isEmpty() { return count==0; } public int Length() { return itemArray.length; } }