logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
ArraysBoyer-Moore Majority Vote AlgorithmKadane’s AlgorithmTriple Reverse algorithm aka Reversal Algorithm
Cycle detectionDijkstraDisjoint Set UnionGraphTopological SortTraversal algorithmsTree Traversal
Binary HeapBinomial HeapHeap
Cycle DetectionLinked ListSorting
Finding the majority elementRotate array by K steps
Finding If Number is PrimeGCD of two numbersPerforming ExponentiationPrime NumbersRandomising Data
3. Max Consecutive OnesHoare'S Partition SchemeLomuto Partition Scheme
DequesPriority QueueQueue
The Binary Search Master GuideThe Rotated Sorted Array Master Guide
Bubble SortCounting SortInsertion SortMerge SortPigeon Hole SortQuick SortSelection Sort
Monotonic StackMonotonic StackStackReversing an ArrayStackTower of Hanoi
Binary Tree TraversalCentre of gravityDiameter of TreeShortest Path
Algorithmic Pattern/queue

Queue

2 min read
On this pageLinked-List Queues <a href="#head-3-59" id="head-3-59"></a>
Linked-List Queues <a href="#head-3-59" id="head-3-59"></a>Array Queues <a href="#head-3-60" id="head-3-60"></a>

A queue is a data structure where items are added and removed in first-in-first-out order. Because of this first-in-first-out (FIFO, usually pronounced “fife-oh”) behavior, stacks are sometimes called FIFO lists or FIFOs.

The method that adds an item to a queue is called Enqueue, and the item that removes an item from a queue is called Dequeue.

# Linked-List Queues

The Enqueue method simply adds a new cell to the top of the list, and the Dequeue method removes the bottom cell from the list.

<mark style="background-color:yellow;">To make removing the last item from the queue easy, the queue should use a doubly linked list.</mark> With a doubly linked list, enqueueing and dequeueing items have O(1)\text{O}(1)O(1) run times.

# Array Queues

To keep track of the array positions that are in use, use two variables:

  • Next, to mark the next open position
  • Last, to mark the position that has been in use the longest

If we simply store items at one end of an array and remove them from the other, however, the occupied spaces move down through the array.

A more effective approach is to build a circular array, in which we treat the last item as if it were immediately before the first item. Now when Next falls off the end of the array, it wraps around to the first position, and the program can store new items there.

Since when the queue is empty, Next is the same as Last and in case of full queue Next goes all the way around the array and catches up to Last , so there's no obvious way to tell whether the queue is empty or full. Solution are to

  • track the number of items in the queue
  • keep track of the number of unused spaces in the queue,
  • keep track of the number of items added to and removed from the queue

If the queue is full and to add more items, allocate a larger storage array, copy the data into the new array, and then use the new array instead of the old one.

←PreviousPriority QueueNext→The Binary Search Master Guide

On this page

Linked-List Queues <a href="#head-3-59" id="head-3-59"></a>Array Queues <a href="#head-3-60" id="head-3-60"></a>