Class Queue2<T>

java.lang.Object
components.queue.QueueSecondary<T>
components.queue.Queue2<T>
Type Parameters:
T - type of Queue entries
All Implemented Interfaces:
Queue<T>, QueueKernel<T>, Standard<Queue<T>>, Iterable<T>

public class Queue2<T> extends QueueSecondary<T>
Queue represented as a singly linked list, done "bare-handed", with implementations of primary methods.

Execution-time performance of all methods implemented in this class is O(1).

Representation Invariant (concrete invariant of $this):
$this.length >= 0  and
[$this.preFront is not null]  and
[$this.rear is not null]  and
[$this.preFront points to the first node of a singly linked list
 containing $this.length + 1 nodes]  and
[$this.rear points to the last node in that singly linked list]  and
[$this.rear.next is null]
Abstraction Relation (interpretation mapping between $this and this):
this = [data in nodes starting at $this.preFront.next and
 running through $this.rear]
  • Constructor Details

    • Queue2

      public Queue2()
      No-argument constructor.
  • Method Details

    • newInstance

      public final Queue<T> newInstance()
      Description copied from interface: Standard
      Returns a new object with the same dynamic type as this, having an initial value. If the type T has a no-argument constructor, then the value of the new returned object satisfies the contract of the no-argument constructor for T. If T does not have a no-argument constructor, then the value of the new returned object satisfies the contract of the constructor call that was used to initialize this .
      Returns:
      new object "like" this with an initial value
    • clear

      public final void clear()
      Description copied from interface: Standard
      Resets this to an initial value. If the type T has a no-argument constructor, then this satisfies the contract of the no-argument constructor for T. If T does not have a no-argument constructor, then this satisfies the contract of the constructor call that was used to initialize #this.
    • transferFrom

      public final void transferFrom(Queue<T> source)
      Description copied from interface: Standard
      Sets this to the incoming value of source, and resets source to an initial value; the declaration notwithstanding, the dynamic type of source must be the same as the dynamic type of this. If the type T has a no-argument constructor, then source satisfies the contract of the no-argument constructor for T. If T does not have a no-argument constructor, then source satisfies the contract of the constructor call that was used to initialize #source.
      Parameters:
      source - object whose value is to be transferred
    • enqueue

      public final void enqueue(T x)
      Description copied from interface: QueueKernel
      Adds x to the end of this.
      Parameters:
      x - the entry to be added
    • dequeue

      public final T dequeue()
      Description copied from interface: QueueKernel
      Removes and returns the entry at the front of this.
      Returns:
      the entry removed
    • length

      public final int length()
      Description copied from interface: QueueKernel
      Reports length of this.
      Returns:
      the length of this
    • iterator

      public final Iterator<T> iterator()
    • replaceFront

      public final T replaceFront(T x)
      Description copied from interface: Queue
      Replaces the front of this with x, and returns the old front.
      Specified by:
      replaceFront in interface Queue<T>
      Overrides:
      replaceFront in class QueueSecondary<T>
      Parameters:
      x - the new front entry
      Returns:
      the old front entry