Select Git revision
UserTest.php
MyStack.java 861 B
public class MyStack {
private Node top;
public MyStack() {
super();
top = new Node(null, null);
}
public int size() {
return top.getHeight();
}
public void push(Object element) {
this.top = new Node(element, top);
}
public Object pop() {
Object payload = top.getPayload();
top = top.getPrevious();
return payload;
}
private class Node {
private Object payload;
private int height;
private Node previous;
Node(Object payload, Node oldTop) {
this.payload = payload;
this.previous = oldTop;
height = (oldTop == null) ? 0 : oldTop.height+1;
}
Object getPayload() {return payload;}
int getHeight() {return height;}
Node getPrevious() {return previous;}
}
}