Skip to content

Instantly share code, notes, and snippets.

@AndrejJurkin
Last active June 18, 2017 17:09
Show Gist options
  • Save AndrejJurkin/a9c75538ae73e05c11d8684501153785 to your computer and use it in GitHub Desktop.
Save AndrejJurkin/a9c75538ae73e05c11d8684501153785 to your computer and use it in GitHub Desktop.
Stack implementation using linked list
public class Stack<T> {
private static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
}
private Node<T> top;
public T pop() {
if (top == null) {
return null;
}
T item = top.data;
top = top.next;
return item;
}
public void push(T data) {
Node<T> newTop = new Node<>(item);
newTop.next = top;
top = newTop;
}
public T peek() {
if (top == null) {
return null;
}
return top.data;
}
public boolean isEmpty() {
return top == null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment