Class SizeLimitedStack<T>


  • public class SizeLimitedStack<T>
    extends java.lang.Object
    A stack with limited size. If the stack is full, the oldest element will be removed when new element was pushed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.LinkedList<T> list  
      private int sizeLimit  
    • Constructor Summary

      Constructors 
      Constructor Description
      SizeLimitedStack​(int sizeLimit)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Empty the stack.
      T peek()
      Looks at the object at the top of this stack without removing it from the stack.
      T pop()
      Removes the object at the top of this stack and returns that object as the value of this function.
      void push​(T e)
      Pushes an item onto the top of this stack.
      int size()
      Returns the number of elements in this stack.
      java.lang.Object[] toArray()
      Return an array of all elements in the stack.
      • Methods inherited from class java.lang.Object

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

      • list

        private java.util.LinkedList<T> list
      • sizeLimit

        private int sizeLimit
    • Constructor Detail

      • SizeLimitedStack

        public SizeLimitedStack​(int sizeLimit)
        Constructor
        Parameters:
        sizeLimit - the maximum number of elements in the stack. If the stack is full, the oldest element will be removed when new element was pushed.
    • Method Detail

      • push

        public void push​(T e)
        Pushes an item onto the top of this stack.
        Parameters:
        e - the item to be pushed onto this stack.
      • pop

        public T pop()
        Removes the object at the top of this stack and returns that object as the value of this function.
        Returns:
        The object at the top of this stack (the last item of the Vector object).
        Throws:
        java.util.NoSuchElementException - if this list is empty
      • peek

        public T peek()
        Looks at the object at the top of this stack without removing it from the stack.
        Returns:
        the object at the top of this stack (the last item of the Vector object).
        Throws:
        java.util.NoSuchElementException - if this list is empty
      • clear

        public void clear()
        Empty the stack.
      • toArray

        public java.lang.Object[] toArray()
        Return an array of all elements in the stack. The oldest element is the first element of the returned array.
        Returns:
        the array contained all elements in the stack.
      • size

        public int size()
        Returns the number of elements in this stack.
        Returns:
        the number of elements in this stack