RosettaCodeData/Task/Stack/Java/stack-2.java

30 lines
704 B
Java
Raw Normal View History

2014-01-17 05:32:22 +00:00
public class Stack{
2013-04-11 01:07:29 -07:00
private Node first = null;
public boolean isEmpty(){
return first == null;
}
2014-01-17 05:32:22 +00:00
public Object Pop(){
2013-04-11 01:07:29 -07:00
if(isEmpty())
throw new Exception("Can't Pop from an empty Stack.");
else{
2014-01-17 05:32:22 +00:00
Object temp = first.value;
2013-04-11 01:07:29 -07:00
first = first.next;
return temp;
}
}
2014-01-17 05:32:22 +00:00
public void Push(Object o){
2013-04-11 01:07:29 -07:00
first = new Node(o, first);
}
class Node{
public Node next;
2014-01-17 05:32:22 +00:00
public Object value;
public Node(Object value){
2013-04-11 01:07:29 -07:00
this(value, null);
}
2014-01-17 05:32:22 +00:00
public Node(Object value, Node next){
2013-04-11 01:07:29 -07:00
this.next = next;
this.value = value;
}
}
}