Last active
June 18, 2017 17:09
-
-
Save AndrejJurkin/a9c75538ae73e05c11d8684501153785 to your computer and use it in GitHub Desktop.
Stack implementation using linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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