Queue Operations
Table of Contents + β
Picture the line at a coffee shop. The person who came first gets served first. A new person joins at the back and waits their turn. A queue in code works just like that line. So the real question is, how do we actually use it? What can we do to a queue? That is what this page is about. We will go through every basic move you can make on a queue, one at a time.
A queue follows FIFO, which means First In First Out. The first item you put in is the first one to come out. So all our operations respect that one rule.
π― The Core Queue Operations
Before we touch any code, let me show you the small set of moves a queue gives you. That is all a queue really is. Just a few simple operations.
Here is the list:
- enqueue β add an item to the back of the queue.
- dequeue β remove the item from the front and give it back.
- front (also called peek) β look at the front item without removing it.
- isEmpty β check if the queue has nothing in it.
- size β count how many items are waiting.
That is the whole toolkit. Let us walk through each one and see the steps.
π₯ Enqueue: Add to the Back
Enqueue means you add a new item. It always goes to the back of the queue, never the middle, never the front. Just like a new person joining the end of the coffee shop line.
Think about the pain this solves. Items keep coming in and you need to serve them in the same order they arrived. Enqueue is how a new item joins the line and waits its turn.
Steps to enqueue an item:
- Take the new item you want to add.
- Place it right after the current last item.
- The back of the queue now points to this new item.
- The size goes up by one.
Here is a small picture of three enqueues happening one after another.
The item 10 came first so it sits at the front. The item 30 came last so it waits at the back.
π€ Dequeue: Remove from the Front
Dequeue is the opposite. You take the item out from the front, because that item has been waiting the longest. It came first, so it leaves first. After it leaves, the next item becomes the new front.
Steps to dequeue an item:
- First check if the queue is empty. If it is, there is nothing to remove.
- Look at the front item.
- Remove it and hold its value to return.
- Move the front to the next item.
- The size goes down by one.
Caution
Always check isEmpty before you dequeue. If you try to remove from an empty queue, your program can crash or give a wrong value. This mistake is called underflow.
π Front, isEmpty and Size
These three are the smaller helpers. They do not change the queue. They just tell you something about it.
- front gives you the value at the front without removing it. This is handy when you want to see who is next but not serve them yet.
- isEmpty returns true when the queue has no items. You call this before every dequeue to stay safe.
- size tells you how many items are sitting in the queue right now.
None of these touch the order. They only read.
π» Queue Operations in Code
Now let us see it run. Most languages already give you a ready-made queue, so we will use the built-in one where we can. We will enqueue a few numbers, peek at the front, dequeue a couple, and print the size along the way.
In C there is no built-in queue. So we use a simple array with a front and rear index. The other languages have proper queue types we can use directly.
from collections import deque
# deque is the recommended way to make a queue in Pythonq = deque()
# append adds an item to the back (enqueue)q.append(10)q.append(20)q.append(30)
# q[0] reads the front item without removing itprint("Front item:", q[0])print("Size:", len(q))
# popleft removes and returns the front item (dequeue)print("Dequeued:", q.popleft())print("Dequeued:", q.popleft())
print("Front item:", q[0])print("Size:", len(q))print("Is empty:", len(q) == 0)The output of the above code will be:
Front item: 10Size: 3Dequeued: 10Dequeued: 20Front item: 30Size: 1Is empty: FalseNote
In JavaScript, shift() on an array is easy to read but it is slow for very large queues. Every remaining item has to shift one spot to the left. For real heavy use, a linked-list based queue or a two-pointer approach keeps it fast.
β±οΈ How Fast Are These Operations?
The good news is that a proper queue is fast. With the right setup, adding and removing both take constant time. Constant time means the work does not grow even when the queue gets huge. We write that as O(1).
Here is each operation and its cost.
| Operation | What it does | Time |
|---|---|---|
| enqueue | Add to the back | O(1) |
| dequeue | Remove from the front | O(1) |
| front / peek | Read the front item | O(1) |
| isEmpty | Check if empty | O(1) |
| size | Count the items | O(1) |
Tip
Both enqueue and dequeue are O(1) when you use a proper queue, like std::queue in C++, deque in Python, or a LinkedList queue in Java. The trap is removing from the front of a plain array, which can become O(n) because the rest of the items shift over. Pick the right tool and you stay fast.
β οΈ Common Mistakes to Avoid
A few small slips trip up almost everyone at the start. Watch out for these.
- Dequeuing without checking
isEmptyfirst. This causes underflow and can crash your program. - Mixing up front and back. Remember, items join at the back and leave from the front.
- Using
shift()on a giant JavaScript array and wondering why it got slow. That front removal is O(n). - Forgetting that
peekandfrontonly look. They do not remove anything. - Thinking
sizechanges the queue. It only counts. It never touches the items.
π§© What Youβve Learned
β A queue follows FIFO, so the first item in is the first item out.
β The core operations are enqueue, dequeue, front, isEmpty and size.
β Enqueue adds to the back, and dequeue removes from the front.
β
Always check isEmpty before a dequeue to avoid underflow.
β Most languages have a built-in queue you can use directly.
β With a proper queue, enqueue and dequeue both run in O(1) constant time.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
In a queue that follows FIFO, which item leaves first?
Why: FIFO means First In First Out, so the item that was added first is the one removed first.
- 2
Which operation adds a new item to the back of the queue?
Why: Enqueue places a new item at the back of the queue, just like joining the end of a line.
- 3
Why should you call isEmpty before a dequeue?
Why: Dequeuing from an empty queue causes underflow, which can crash the program or return a wrong value.
- 4
With a proper queue, what is the time cost of enqueue and dequeue?
Why: A proper queue adds and removes in constant time O(1), because the work does not grow with the number of items.