How to Work with Queues in TypeScript
A queue is a collection of items arranged in a First-In-First-Out (FIFO) order. This means that the first item added is the first to be removed, much like a supermarket line where customers are served in the order they arrive. In this hands-on tutor...

A queue is a collection of items arranged in a First-In-First-Out (FIFO) order. This means that the first item added is the first to be removed, much like a supermarket line where customers are served in the order they arrive.
In this hands-on tutorial, you will learn how to implement queues in TypeScript using linked lists.
Here’s what we’ll cover
Prerequisites
TypeScript: You need to know TypeScript basics, such as interfaces, types, and classes.
Algorithm fundamentals: You need a basic understanding of data structures and algorithms. For example, you should be comfortable analyzing time and space complexity using Big-O notation.
Linked Lists Data Structure: It's important to have a solid understanding of linked lists before starting this tutorial. I wrote a detailed linked list tutorial that you can use to learn about this data structure.
Getting Started
To get started with this tutorial, you’ll use a playground project that’s designed to help you implement queues and follow each step hands-on.
Clone the project from the GitHub repository and code along with the tutorial.
The project structure is as follows:
.
├── index.ts
├── examples
│ ├── 01-linked-list.ts
│ ├── 02-simple-queue.ts
│ ├── 03-circular-queue.ts
│ ├── 04-double-ended-queue.ts
│ └── 05-priority-queue.ts
└── playground
├── 01-linked-list.ts
├── 02-simple-queue.ts
├── 03-circular-queue.ts
├── 04-double-ended-queue.ts
└── 05-priority-queue.ts
Throughout the tutorial, you will use the playground
directory to implement and test your code.
The examples
directory contains the final version of each implementation. If you get stuck, you can look at these solutions as a last resort!
What Are Queues?
A queue is a data structure that manages items in a first-in, first-out (FIFO) order, where the first item added is the first removed.
For example, imagine a printer handling jobs. If you send three documents to print, the printer processes them in the order they arrive. The first document prints first, then the second, and finally the third.
In programming, queues help manage tasks that need to happen in order, such as:
A web server queues incoming requests to process them one by one.
A chat app queues messages to send them in the order they’re typed.
A navigation app queues locations to explore a map level by level. (Breadth-First Search)
There are four types of queues in a data structure:
Simple Queue: Adds items to the back and removes them from the front in first-in, first-out (FIFO) order.
Circular Queue: It is similar to a simple queue, except the last element is connected to the first.
Double-Ended Queue (Deque): Allows adding or removing items from both front and back, like a bus stop line where people join or leave either end.
Priority Queue: Processes items based on priority, not arrival order. Like a delivery app processes VIP orders before regular ones.
Each of these queues has a set of operations for managing their items. In this tutorial, you will learn about the following common and widely used operations:
enqueue: Adds an item to the back of the queue, like a new customer joining the end of a ticket line.
dequeue: Removes and returns the item at the front of the queue.
getFront: Looks at the item at the front without removing it, like checking who’s first in line.
getRear: Looks at the item at the back without removing it, like seeing who’s last in line.
isEmpty: Checks if the queue has no items.
isFull: Checks if the queue has reached its maximum size.
peek: Same as
getFront
, views the front item without removing it, like a quick glance at the first task.size: Returns the number of items in the queue, like counting how many people are in line.
Now that you know about queues and their main operations, let's get into the actual implementation and see how it looks in code.
There are a few different ways to implement queues, but in this tutorial, you will learn about linked list-based queues, which use a linked list to create the queues.
First, let's briefly learn about the linked list data structure and then move on to the queue implementation.
What Are Linked Lists?
A linked list is a method of storing a collection of items where each item, known as a "node," contains two parts: the actual data and a reference (or pointer) to the next item in the list.
Unlike arrays, where all items are stored next to each other in memory, linked lists connect nodes using these references, like a chain.
Linked lists are used to implement queues because they allow efficient insertion at the end and removal from the front, which are the two main operations of a queue.
In a linked list-based queue, you can add a new node at the tail and remove one from the head in constant time (O(1)
) without needing to shift elements, as you would in an array.
In this tutorial, you are going to use a specific type of linked list called Circular Doubly Linked List.
A circular doubly linked list is a type of linked list where each node connects to both the next and previous nodes, and the last node loops back to the first one to form a circle.
This means you can move through the list in both directions and never hit a dead end. This makes it easy to go forward or backward through nodes and helps avoid special cases like handling null
at the ends.
In a circular doubly linked list, everything is connected in a loop, which simplifies certain queue operations and keeps things efficient.
You can learn more about circular linked lists in my Linked Lists Handbook.
For this tutorial, I’ve already added a circular doubly linked list in src/playground/01-linked-list.ts
:
//