Skip to content
Snippets Groups Projects
Commit 77eb46e5 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Robust backing structure, robust size() & push()

parent f2a86edc
Branches
No related tags found
No related merge requests found
public class MyStack { public class MyStack {
private Object element; private Node top;
public MyStack() { public MyStack() {
super(); super();
element = null; top = new Node(null, null);
} }
public int size() { public int size() {
return (element == null) ? 0 : 1; return top.getHeight();
} }
public void push(Object element) { public void push(Object element) {
this.element = element; this.top = new Node(element, top);
}
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;}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment