Code example for Stack

Methods: empty, peek, pop, push

0
        stack.push(iterator);
    } 
 
    public Object next() {
        if (hasNext()) { 
            Iterator iterator = (Iterator) stack.peek();
            MenuComponent component = (MenuComponent) iterator.next();
            if (component instanceof Menu) {
                stack.push(((Menu) component).createIterator());
            } 
            return component;
        } else { 
            return null; 
        } 
    } 
 
    public boolean hasNext() { 
        if (stack.empty()) {
            return false; 
        } else { 
            Iterator iterator = (Iterator) stack.peek();
            if (!iterator.hasNext()) {
                stack.pop();
                return hasNext(); 
            } else { 
                return true; 
            } 
        }