|
| 1 | +/** |
| 2 | + * This class implements a Stack using a regular array. |
| 3 | + * <p> |
| 4 | + * A stack is exactly what it sounds like. An element gets added to the top of |
| 5 | + * the stack and only the element on the top may be removed. This is an example |
| 6 | + * of an array implementation of a Stack. So an element can only be added/removed |
| 7 | + * from the end of the array. In theory stack have no fixed size, but with an |
| 8 | + * array implementation it does. |
| 9 | + * |
| 10 | + * @author Unknown |
| 11 | + */ |
| 12 | +public class StackArray { |
| 13 | + |
| 14 | + /** |
| 15 | + * Main method |
| 16 | + * |
| 17 | + * @param args Command line arguments |
| 18 | + */ |
| 19 | + public static void main(String[] args) { |
| 20 | + // Declare a stack of maximum size 4 |
| 21 | + StackArray myStackArray = new StackArray(4); |
| 22 | + |
| 23 | + // Populate the stack |
| 24 | + myStackArray.push(5); |
| 25 | + myStackArray.push(8); |
| 26 | + myStackArray.push(2); |
| 27 | + myStackArray.push(9); |
| 28 | + |
| 29 | + System.out.println("*********************Stack Array Implementation*********************"); |
| 30 | + System.out.println(myStackArray.isEmpty()); // will print false |
| 31 | + System.out.println(myStackArray.isFull()); // will print true |
| 32 | + System.out.println(myStackArray.peek()); // will print 9 |
| 33 | + System.out.println(myStackArray.pop()); // will print 9 |
| 34 | + System.out.println(myStackArray.peek()); // will print 2 |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * The max size of the Stack |
| 39 | + */ |
| 40 | + private int maxSize; |
| 41 | + |
| 42 | + /** |
| 43 | + * The array representation of the Stack |
| 44 | + */ |
| 45 | + private int[] stackArray; |
| 46 | + |
| 47 | + /** |
| 48 | + * The top of the stack |
| 49 | + */ |
| 50 | + private int top; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor |
| 54 | + * |
| 55 | + * @param size Size of the Stack |
| 56 | + */ |
| 57 | + public StackArray(int size) { |
| 58 | + maxSize = size; |
| 59 | + stackArray = new int[maxSize]; |
| 60 | + top = -1; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds an element to the top of the stack |
| 65 | + * |
| 66 | + * @param value The element added |
| 67 | + */ |
| 68 | + public void push(int value) { |
| 69 | + if (!isFull()) { // Checks for a full stack |
| 70 | + top++; |
| 71 | + stackArray[top] = value; |
| 72 | + } else { |
| 73 | + resize(maxSize * 2); |
| 74 | + push(value); // don't forget push after resizing |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Removes the top element of the stack and returns the value you've removed |
| 80 | + * |
| 81 | + * @return value popped off the Stack |
| 82 | + */ |
| 83 | + public int pop() { |
| 84 | + if (!isEmpty()) { // Checks for an empty stack |
| 85 | + return stackArray[top--]; |
| 86 | + } |
| 87 | + |
| 88 | + if (top < maxSize / 4) { |
| 89 | + resize(maxSize / 2); |
| 90 | + return pop();// don't forget pop after resizing |
| 91 | + } else { |
| 92 | + System.out.println("The stack is already empty"); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns the element at the top of the stack |
| 99 | + * |
| 100 | + * @return element at the top of the stack |
| 101 | + */ |
| 102 | + public int peek() { |
| 103 | + if (!isEmpty()) { // Checks for an empty stack |
| 104 | + return stackArray[top]; |
| 105 | + } else { |
| 106 | + System.out.println("The stack is empty, cant peek"); |
| 107 | + return -1; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private void resize(int newSize) { |
| 112 | + // private int[] transferArray = new int[newSize]; we can't put modifiers here ! |
| 113 | + int[] transferArray = new int[newSize]; |
| 114 | + |
| 115 | + // for(int i = 0; i < stackArray.length(); i++){ the length isn't a method . |
| 116 | + for (int i = 0; i < stackArray.length; i++) { |
| 117 | + transferArray[i] = stackArray[i]; |
| 118 | + stackArray = transferArray; |
| 119 | + } |
| 120 | + maxSize = newSize; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns true if the stack is empty |
| 125 | + * |
| 126 | + * @return true if the stack is empty |
| 127 | + */ |
| 128 | + public boolean isEmpty() { |
| 129 | + return (top == -1); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns true if the stack is full |
| 134 | + * |
| 135 | + * @return true if the stack is full |
| 136 | + */ |
| 137 | + public boolean isFull() { |
| 138 | + return (top + 1 == maxSize); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Deletes everything in the Stack |
| 143 | + * <p> |
| 144 | + * Doesn't delete elements in the array |
| 145 | + * but if you call push method after calling |
| 146 | + * makeEmpty it will overwrite previous |
| 147 | + * values |
| 148 | + */ |
| 149 | + public void makeEmpty() { // Doesn't delete elements in the array but if you call |
| 150 | + top = -1; // push method after calling makeEmpty it will overwrite previous values |
| 151 | + } |
| 152 | +} |
0 commit comments