Class BoundedIntQueue

java.lang.Object
it.unimi.di.prog2.s10.BoundedIntQueue

public class BoundedIntQueue extends Object
A queue is a mutable data structure that provides access to its elements in first-in/first-out order.

A bounded queue has an upper bound, established when a queue is created, on the number of elements that can be stored in the queue.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int[]
    The elements in the queue.
    private int
    The index in elements of the first queue element (or -1 if the queue is empty).
    private int
    The index of the first free position in elements (if the queue is not full).
  • Constructor Summary

    Constructors
    Constructor
    Description
    BoundedIntQueue(int capacity)
    Creates a new bounded queue with the given capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Removes the element at the head of the queue.
    void
    enqueue(int x)
    Adds an element to the queue.
    boolean
     
    int
     
    boolean
    Determines whether the queue is empty, i.e., it does not contain any integer.
    boolean
    Determines whether the queue is full, i.e., it contains as many integers as its capacity.
    int
    Returns the number of elements in the queue.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • elements

      private int[] elements
      The elements in the queue.
    • tail

      private int tail
      The index of the first free position in elements (if the queue is not full).
  • Constructor Details

    • BoundedIntQueue

      public BoundedIntQueue(int capacity)
      Creates a new bounded queue with the given capacity.
      Parameters:
      capacity - the capacity of the queue.
      Throws:
      IllegalArgumentException - if capacity is not positive.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Determines whether the queue is empty, i.e., it does not contain any integer.
      Returns:
      true if the queue is empty, false otherwise.
    • isFull

      public boolean isFull()
      Determines whether the queue is full, i.e., it contains as many integers as its capacity.
      Returns:
      true if the queue is full, false otherwise.
    • size

      public int size()
      Returns the number of elements in the queue.
      Returns:
      the number of elements.
    • enqueue

      public void enqueue(int x)
      Adds an element to the queue.
      Parameters:
      x - the element to add.
      Throws:
      IllegalStateException - if the queue is full.
    • dequeue

      public int dequeue()
      Removes the element at the head of the queue.
      Returns:
      the element at the head of the queue.
      Throws:
      NoSuchElementException - if the queue is empty.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object