diff --git a/java/stack/ArrayStack.java b/java/stack/ArrayStack.java new file mode 100644 index 000000000..d43a88774 --- /dev/null +++ b/java/stack/ArrayStack.java @@ -0,0 +1,88 @@ +import java.util.EmptyStackException; + +/** + * An array implementation of the Stack interface. + */ +public class ArrayStack implements Stack { + + private static final int DEFAULT_CAPACITY = 10; + + private T[] array; + private int top; + + /** + * Constructs an empty stack with the default capacity. + */ + public ArrayStack() { + this(DEFAULT_CAPACITY); + } + + /** + * Constructs an empty stack with the specified capacity. + * + * @param capacity the initial capacity of the stack + */ + public ArrayStack(int capacity) { + array = (T[]) new Object[capacity]; + top = -1; + } + + /** + * {@inheritDoc} + */ + @Override + public void push(T element) { + if (top == array.length - 1) { + expandCapacity(); + } + top++; + array[top] = element; + } + + /** + * {@inheritDoc} + */ + @Override + public T pop() { + if (isEmpty()) { + throw new EmptyStackException(); + } + T element = array[top]; + array[top] = null; + top--; + return element; + } + + /** + * {@inheritDoc} + */ + @Override + public T peek() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return array[top]; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isEmpty() { + return top == -1; + } + + /** + * {@inheritDoc} + */ + @Override + public int size() { + return top + 1; + } + + private void expandCapacity() { + T[] newArray = (T[]) new Object[array.length * 2]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } +} diff --git a/java/stack/LinkedStack b/java/stack/LinkedStack new file mode 100644 index 000000000..466ac8a82 --- /dev/null +++ b/java/stack/LinkedStack @@ -0,0 +1,79 @@ +/** + * A linked implementation of the Stack interface. + */ +public class LinkedStack implements Stack { + + private static class Node { + private T data; + private Node next; + + public Node(T data) { + this.data = data; + this.next = null; + } + } + + private Node top; + private int size; + + /** + * Constructs an empty LinkedStack. + */ + public LinkedStack() { + top = null; + size = 0; + } + + /** + * {@inheritDoc} + */ + @Override + public void push(T element) { + Node newNode = new Node<>(element); + newNode.next = top; + top = newNode; + size++; + } + + /** + * {@inheritDoc} + */ + @Override + public T pop() { + if (isEmpty()) { + throw new EmptyStackException(); + } + T element = top.data; + top = top.next; + size--; + return element; + } + + /** + * {@inheritDoc} + */ + @Override + public T peek() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return top.data; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * {@inheritDoc} + */ + @Override + public int size() { + return size; + } + +} diff --git a/java/stack/Stack.java b/java/stack/Stack.java new file mode 100644 index 000000000..14336ca8e --- /dev/null +++ b/java/stack/Stack.java @@ -0,0 +1,52 @@ +/** + * The Stack interface represents a last-in-first-out (LIFO) stack of elements. + */ +public interface Stack { + + /** + * Returns the number of elements in the stack. + * + * Complexity: O(1) + * + * @return the number of elements in the stack + */ + int size(); + + /** + * Returns true if the stack is empty, false otherwise. + * + * Complexity: O(1) + * + * @return true if the stack is empty, false otherwise + */ + boolean isEmpty(); + + /** + * Pushes an element onto the top of the stack. + * + * Complexity: O(1) + * + * @param element the element to be pushed onto the stack + */ + void push(E element); + + /** + * Removes and returns the element at the top of the stack. + * + * Complexity: O(1) + * + * @return the element at the top of the stack + * @throws EmptyStackException if the stack is empty + */ + E pop(); + + /** + * Returns the element at the top of the stack without removing it. + * + * Complexity: O(1) + * + * @return the element at the top of the stack + * @throws EmptyStackException if the stack is empty + */ + E peek(); +}