Digital Garden
Computer Science
Algorithms & Data Structures
Queues

Queues

A queue is as the name says like a queue of people. Meaning it follows the FIFO policy (first in first out). The most common operations on queues are:

  • enqueue(E e): Adds an element to the rear of the queue.
  • E dequeue(): Takes the element from the front of the queue.
  • E peek(): Returns the element at the front of the queue, which corresponds to the element to next be dequeued.
Visualization of a queue.

Implementing a Queue

Todo
MyQueue.java
// TODO

Queue Using two Stacks

Although the most common way of implementing a queue is with a linked list it is also possible to implement a queue by using two stacks. Just like when implementing a stack with two queues you need to decide if adding or removing an element will be expensive.