Skip to content
Snippets Groups Projects
Select Git revision
  • f40123b741af3fca8efa00dd57740ed6bc8bab15
  • master default
2 results

annotate_functions.js

Blame
  • 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;}
        }
    }