Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack in Java #589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions java/stack/ArrayStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.util.EmptyStackException;

/**
* An array implementation of the Stack interface.
*/
public class ArrayStack<T> implements Stack<T> {

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;
}
}
79 changes: 79 additions & 0 deletions java/stack/LinkedStack
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* A linked implementation of the Stack interface.
*/
public class LinkedStack<T> implements Stack<T> {

private static class Node<T> {
private T data;
private Node<T> next;

public Node(T data) {
this.data = data;
this.next = null;
}
}

private Node<T> top;
private int size;

/**
* Constructs an empty LinkedStack.
*/
public LinkedStack() {
top = null;
size = 0;
}

/**
* {@inheritDoc}
*/
@Override
public void push(T element) {
Node<T> 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;
}

}
52 changes: 52 additions & 0 deletions java/stack/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* The Stack interface represents a last-in-first-out (LIFO) stack of elements.
*/
public interface Stack<E> {

/**
* 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();
}