Last active
December 20, 2015 21:09
-
-
Save petehuang/6195862 to your computer and use it in GitHub Desktop.
Stack implementation in Java
This file contains 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 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