diff --git a/stack/AStack.java b/stack/AStack.java new file mode 100644 index 0000000..0c14e71 --- /dev/null +++ b/stack/AStack.java @@ -0,0 +1,42 @@ +/** Array-based stack implementation */ +class AStack implements Stack { + private static final int defaultSize = 10; + + private int maxSize; // Maximum size of stack + private int top; // Index for top Object + private E[] listArray; // Array holding stack + + /** Constructors */ + AStack() { this(defaultSize); } + + @SuppressWarnings("unchecked") // Generic array allocation + AStack(int size) { + maxSize = size; + top = 0; + listArray = (E[])new Object[size]; // Create listArray + } + + /** Reinitialize stack */ + public void clear() { top = 0; } + + /** Push "it" onto stack */ + public void push(E it) { + assert top != maxSize : "Stack is full"; + listArray[top++] = it; + } + + /** Remove and pop element */ + public E pop() { + assert top != 0 : "Stack is empty"; + return listArray[--top]; + } + + /** @return Top element */ + public E topValue() { + assert top != 0 : "Stack is empty"; + return listArray[top - 1]; + } + + /** @return Stack size */ + public int length() { return top; } +}