Skip to content

Instantly share code, notes, and snippets.

@petehuang
Last active December 20, 2015 21:09
Show Gist options
  • Save petehuang/6195862 to your computer and use it in GitHub Desktop.
Save petehuang/6195862 to your computer and use it in GitHub Desktop.
Stack implementation in Java
public class Element {
private int value;
private Element next;
public Element(int value, Element next) {
this.value = value;
this.next = next;
}
}
public class Stack {
private int number;
private Element head;
public void push(int value) {
head = new Element(value, head);
}
public int pop() {
if(isEmpty()) return "Stack is empty, sorry.";
int result = head.value;
head = head.next;
return result;
}
public boolean isEmpty() {
return head == null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment