Definition

Stores a collection of elements that can only be modified by adding elements at one end of the sequence and removing at the other end - FIFO

Main operations

  • enqueue and dequeue
  • front, isFull, isEmpty

Implementation

Singly vs. doubly linked lists for queues

Since nodes in a normal singly linked list don’t have a previous reference, when enqueuing, it takes time to traverse to and update the the second-last node’s next reference to the added node (Presuming the head points to the front of the queue)

Doubly-linked list

Modified singly-linked list

This implementation is efficient because adding elements to the tail of the list and removing elements from the head of the list can be done in constant time.

Circular array

Use a dynamically resizing array with front/back pointers and modulo operations for when the queue wraps around

Circular queue

a data structure that uses a fixed-size array to implement a queue where the front and rear of the queue are connected.

start with a fixed-size array to store the elements of the queue.

We’ll need to keep track of the front and rear of the queue, which will initially both be set to zero.

Restricted deque

Use a deque but impose the restriction that elements can only be inserted at the back of the deque and removed from the front of the deque.

Algorithms using queues


  • task Add implementation time complexity details