Quick Answer
Build a queue data structure in 3 ways. They are a list, a deque and a linked list. Queue follows FIFO order. Enqueue adds at the back and dequeue removes from the front. Deque and linked list take O(1) per dequeue. List takes O(n) per dequeue so avoid it. Test your queue with 3 items and check the order.
Queue data structure is a line of items where the first item in is the first item out. Print jobs and waiting tasks follow this order.
One trap makes a queue data structure crawl on large data. Most tutorials build the queue on a plain list and never measure it.
Python and C++ code follows with timings from real tests. Enqueue and dequeue costs show why the structure under the queue matters.
What Is a Queue Data Structure?
Queue data structure is a collection that adds items at the back and removes them from the front. You take out the first item you added. This order is called first in first out or FIFO.
Think of a line at a ticket counter. Whoever arrives first gets served first and new arrivals wait at the back.
Adding an item is called enqueue and removing one is called dequeue. These two operations are all a queue does. It is the mirror image of a stack, which uses the opposite order.
How Does a Queue Work Step by Step?
Watch items join and exit in the table. Front item shows what the next dequeue removes.
| Step | Queue after the step | Front item |
|---|---|---|
| Start | [ ] | none |
| Enqueue A | [A] | A |
| Enqueue B | [A, B] | A |
| Enqueue C | [A, B, C] | A |
| Dequeue (A exits) | [B, C] | B |
| Dequeue (B exits) | [C] | C |
Items exit in the exact order they arrived. That order is fixed and defines every queue.
Which 3 Ways Build a Queue in Python?
Choose the structure under the queue first. Table below compares enqueue and dequeue costs of the 3 ways before the code.
| Way | Enqueue | Dequeue | Verdict |
|---|---|---|---|
| List | O(1) | O(n) | Avoid for real queues |
| Deque | O(1) | O(1) | Recommended |
| Linked list | O(1) | O(1) | Good practice |
Method #1: List (Slow Dequeue, Avoid)
Who it suits. Learners who test a tiny queue. Extra application. None, because Python is enough.
Python lists look tempting because append and pop(0) already exist. Removing the front item forces every other item to shift one step left.
queue = []
queue.append("A") # enqueue at the back, O(1)
queue.append("B")
queue.append("C")
front = queue.pop(0) # dequeue from the front, O(n)
print(front) # prints A
print(queue) # prints ['B', 'C']
Python documentation says lists are inefficient as queues because popping at the beginning shifts every other element. It recommends collections.deque instead.
Method #2: Deque (Recommended, O(1) Both Ends)
Who it suits. Everyone who needs a working queue in Python. Extra application. None, because deque ships with Python.
from collections import deque
queue = deque()
queue.append("A") # enqueue at the back
queue.append("B")
queue.append("C")
front = queue.popleft() # dequeue from the front, O(1)
print(front) # prints A
print(queue) # prints deque(['B', 'C'])
append adds at the back and popleft removes from the front. Both take O(1) time because a deque is built for both ends.
Method #3: Linked List (O(1) With Your Own Class)
Who it suits. Learners who study how queues work inside. Extra application. None, because Python is enough. Review the linked list post first if nodes are new to you.
Store a head pointer for dequeue and a tail pointer for enqueue. Both ends then take O(1) time.
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedQueue:
def __init__(self):
self.head = None # front of the queue
self.tail = None # back of the queue
self.size = 0
def enqueue(self, value):
node = Node(value)
if self.tail is None:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
self.size += 1
def dequeue(self):
if self.head is None:
raise IndexError("queue is empty")
value = self.head.value
self.head = self.head.next
if self.head is None:
self.tail = None
self.size -= 1
return value
q = LinkedQueue()
for item in ["A", "B", "C"]:
q.enqueue(item)
print(q.dequeue()) # prints A
print(q.dequeue()) # prints B
Heads Up. Dequeue on an empty queue raises an error. Test that case before you trust any queue code.
How Do You Code a Queue Data Structure in C++?
C++ ships a ready queue in the standard library. cppreference describes the queue class as a container adaptor with FIFO behavior and says its default underlying container is the deque class.
#include <iostream>
#include <queue>
int main() {
std::queue<int> q;
q.push(1); // enqueue at the back
q.push(2);
q.push(3);
int front = q.front(); // peek at the front, returns 1
q.pop(); // dequeue the front
std::cout << front << std::endl; // prints 1
return 0;
}
Names differ from Python but the FIFO rule is the same. Enqueue and dequeue map to push and pop. push enqueues and pop dequeues while front only looks at the next item.
Tested. Python 3.13 executed all 3 Python versions and g++ 13.3 compiled the C++ version. Every version printed the order shown in the comments.
Does Front Removal Slow a List Queue?
Yes. Table below shows how a queue data structure behaves when you dequeue every item from a queue of n items. Deque times grow in line with n while list times grow much faster.
| Queue size | List with pop(0) | Deque with popleft | List is slower by |
|---|---|---|---|
| 50,000 | 210.6 ms | 1.2 ms | about 170 times |
| 100,000 | 804.8 ms | 2.4 ms | about 330 times |
| 200,000 | 3,427 ms | 7.3 ms | about 470 times |
Doubling the size makes the list time about 4 times larger. That pattern points to O(n squared) for a full drain. Deque time grows roughly in line with n in this test, which fits O(n).
Times come from a cloud sandbox on Python 3.13 and differ on your computer. Gap grows with queue size on every machine.
What Is the Time Complexity of a Queue Data Structure?
With a deque or a linked list both enqueue and dequeue take O(1) time. Adding to the back and removing from the front take one step no matter how many items wait in line.
| Operation | Deque or linked list | List |
|---|---|---|
| Enqueue | O(1) | O(1) |
| Dequeue | O(1) | O(n) |
| Peek at the front | O(1) | O(1) |
| Space | O(n) | O(n) |
Study the notation in the time complexity and Big O guide if these symbols are new.
How Is a Queue Different From a Stack?
Queue order is FIFO and stack order is LIFO. Choose a queue data structure when the oldest waiting item must go first.
| Detail | Queue | Stack |
|---|---|---|
| Order | First in first out | Last in first out |
| Add at | Back | Top |
| Remove from | Front | Top |
| Everyday example | Line at a counter | Pile of plates |
| Python tool | deque | list |
Print jobs and task schedulers use queues. Queues also power breadth first search, which explores a graph level by level. Open the breadth first search lesson to see that use.
Frequently Asked Questions
Is a Queue First In First Out?
Yes. First item added is first item removed. People call this rule FIFO.
Is Queue Dequeue O(1)?
Yes, when the queue sits on a deque or a linked list. Plain lists make dequeue O(n) because every item shifts left.
Does Python Have a Built In Queue?
Yes. Python has collections.deque for efficient queues and a queue module for threads. Plain lists also work but front removal is slow.
Is a Queue the Same as a Stack?
No. Queues remove the oldest item and stacks remove the newest item. They use opposite ends for removal.
Is a List Good for Queues in Python?
No. Removing the front item shifts every other item and costs O(n). Use collections.deque instead.
Can You Build a Queue With a Linked List?
Yes. Store a head pointer for dequeue and a tail pointer for enqueue so both take O(1) time.
Can You Test a Queue With 3 Items?
Yes. Enqueue 1 then 2 then 3 and dequeue once. Result must be 1. Next item in line must be 2.
What Should You Remember About the Queue Data Structure?
Queue data structure follows first in first out order. Enqueue at the back and dequeue from the front. Build it on a deque or a linked list so both take O(1) time.
Key takeaways.
- List dequeue takes O(n) and deque dequeue takes O(1).
- Dequeue on an empty queue raises an error.
- Stack uses the opposite order of a queue.
Practice question. Enqueue 1 then 2 then 3. Dequeue once and enqueue 4. Find the next item a dequeue removes.
Answer. 2, because 1 exited first and 2 is now at the front.
Open the queue lesson for the reference and solve the queue practice problems.