Class MarkStack<E>


  • public final class MarkStack<E>
    extends java.lang.Object
    A stack with additional operations that support recording the current top of stack as a mark, and then later popping all items pushed since the last mark call.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<E> items  
      private java.util.List<java.lang.Integer> marks  
    • Constructor Summary

      Constructors 
      Constructor Description
      MarkStack()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isEmpty()
      Return true iff the stack is empty.
      void mark()
      Record the current position in the stack for a subsequent popMark call.
      E peek()
      Return the top element of the stack.
      E pop()
      Return the top element of the stack and remove it from the stack.
      java.util.List<E> popMark()
      Return an ordered list of stack elements starting with the element that was on top of the stack when mark was called.
      E push​(E elem)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • items

        private java.util.List<E> items
      • marks

        private java.util.List<java.lang.Integer> marks
    • Constructor Detail

      • MarkStack

        public MarkStack()
    • Method Detail

      • push

        public E push​(E elem)
      • pop

        public E pop()
        Return the top element of the stack and remove it from the stack.
        Throws:
        java.util.EmptyStackException - is thrown if the stack is empty.
        java.lang.IllegalStateException - if an attempt is made to pop past the top mark.
      • isEmpty

        public boolean isEmpty()
        Return true iff the stack is empty.
      • peek

        public E peek()
        Return the top element of the stack. Does not change the stack.
        Throws:
        java.util.EmptyStackException - is thrown if the stack is empty.
      • mark

        public void mark()
        Record the current position in the stack for a subsequent popMark call. This allow marking the start of a related group of items on the stack that can be popped together later by popMark. Multiple mark calls are supported.
      • popMark

        public java.util.List<E> popMark()
        Return an ordered list of stack elements starting with the element that was on top of the stack when mark was called.