Queue and Deque ADT

A. Queue

Queue: Entries organized first-in, first-out (FIFO)

Example: OS Task Queue

Terminology

Specification

queueenqueue(newEntry : T) : voiddequeue(): TgetFront(): TisEmpty(): booleanclear(): void
PseudocodeDescription
enqueueAdds a new entry to the back of the queue
dequeueRemoves and returns the entry at the front of the queue
getFrontRetrieves the queue’s front entry without changing the queue
isEmptyDetects whether the queue is empty
clearRemoves all entries from the queue

Note: Improving performance in implementation

Remember: Client can only see and remove the entry at the front of the queue!

Example: Queue Interface

public interface QueueInterface<T> {
  void enqueue(T: newEntry);
  T dequeue();
  T getFront();
  boolean isEmpty();
  void clear();
}

Simulating a Waiting Line (CRC)

Responsibilities:

Collaboration:

WaitLinelinenumberOfArrivalsnumberServedtotalTimeWaitedsimulate(duration, arrivalProbabilities, max TrascationTime)displayResult()CustomerarrivalTimetransactionTimecustomerNumbergetArrivalTime()getTransactionTime()getCustomerNumber()

Remember: Random Number Generation

Java Class Library: The Interface Queue

Methods:

Java Class Library: The Class AbstractQueue

boolean add(T newEntry);
boolean offer(T newEntry);
T remove();
T poll();
T element();
T peek();
void isEmpty();
int clearSize();

B. Deque

Deque: Double-ended queue.

Example:

Example: Private Node inner-class with next and previous pointers

private class Node<T> {
  private T data;
  private Node next;
  private Node prev;
  private Node (T data) {
      this(data, null, null)
  }
  private Node (T data, Node next, Node prev) {
      this.data = data;
      this.next = next;
      this.prev = prev;
  }
}

Specification

dequeaddToFront(newEntry : T) : booleanaddToBack(newEntry : T) : booleanremoveFront() : booleanremoveBack() : booleangetFront() : TgetBack() : TisEmpty(): booleanclear(): void

Example: Interface

public interface DequeInterface<T> {
  boolean addToFront(T newEntry);
  boolean addToBack(T newEntry);
  boolean removeFront();
  boolean removeBack();
  T getFront();
  T getBack();
  boolean isEmpty();
  void clear();

}

Java Class Library: The Interface Queue

Methods:

Java Class Library: The Class ArrayDeque

Note: A better data structure to implement the deque would be a doubly-linked list.

C. Priority Queues

Priority Queue: Organizes objects according to their priorities.

Note: The definition of “priority” depends on the nature of the items in the queue.

Note: Well be using priority queue (and other ADTs) for the final project (implementing Dijkstra’s algorithm)

Example: Priority Queue Interface

public interface PriorityQueueInterface<T> extends Comparable<? super T> {
  void add(T newEntry);
  T remove();
  T peek();
  boolean isEmpty();
  int getSize();
  void clear();
}

Example: Priority queue based on lexical graphical order

PriorityQueueInterface<String> myQueue = newLinkedPriorityQueue<>();
myQueue.add("Jane");
myQueue.add("Jess");
myQueue.add("Jill");
myQueue.add("Jim");
myQueue.remove();
// Remaining Entries:
// Jess
// Jill
// Jim

Java Class Library: The Class Priority Queue

Methods:

Note: Uses a heap.

Implementations

A. Queue

(Singly) Linked Implementation:

Circular Array Implementation:

Remember: A queue is not a bag, it’s not supposed to be used for storage, we use it because we want to take things out.

Circular Linked Implementation:

B. Deque

Doubly-Linked Implementation:

C. Priority Queue

One way to implement a priority queue is to use an array of queue.