algoritmauzmanı
Sign in
TREN
5Queue
Lesson 5.0

What is a queue?

A is a structure where elements go in at one end and come out at the other. There is a single rule: the first one in is the first one out. A queue works just like a line at the bank. This lesson covers what a queue is, which operations it offers, how it is built and which kinds of queue there are.

What you will learn
  • The FIFO order and how it differs from the LIFO order of a stack
  • The queue operations and the roles of front and rear
  • How a queue is built with an array or a linked list, its kinds and where it is used
5 min

1First in, first out

Think of standing in line at a bank. A newcomer joins the end of the line. When a desk is free, the person at the front is called. Nobody cuts in and nobody skips a turn. A queue works exactly like that. This order is called : First In, First Out.

The same order is everywhere on a computer:

  • The print queue: the documents you send to print line up. The printer prints the one sent first.
  • The keyboard buffer: keys you press while the computer is busy wait in a queue and are handled in the order you pressed them.
  • A call center: callers are put in a waiting line; the first caller is connected first.

A is the opposite: there the last one in is the first one out (). A stack has one end; a queue has two ends, and each one has its own job.

2Two ends: front and rear

The end where elements leave is called the , and the end where they enter is called the . A new element is always added behind rear; the element that leaves is always the one at front. You cannot reach an element in the middle directly.

So queue code keeps track of two places. In an array these are two indexes; in a linked list they are two node addresses. A stack needed only one top.

3A queue is an abstract data type

A queue is defined by the operations it offers, not by how it is built. A definition like this is called an (ADT). Code that uses a queue only knows the operations below; it does not know whether there is an array or a linked list inside.

OperationWhat it doesWhat it returns
enqueue(x)Adds x at the rearNothing (in C, success: true or false)
dequeue()Removes the front elementThe removed element
peek()Looks at the front element without removing itThe front element
isEmpty()Is the queue empty?true or false
isFull()Is the queue full? (only for a fixed-size queue)true or false

In a good implementation all of these take time: however many elements the queue holds, only the two ends are touched. and are the standard names; some books also say "add to the queue" and "remove from the queue".

4Let us trace a sequence of operations

Start with an empty queue and run these operations in order. In the queue column the leftmost element is at the front and the rightmost is at the rear.

OperationQueue (front → rear)Returned value
enqueue(3)3
enqueue(8)3 8
enqueue(5)3 8 5
dequeue()8 53
peek()8 58
enqueue(1)8 5 1
dequeue()5 18
dequeue()15
isEmpty()1false

Look at the values the dequeues returned: 3, 8, 5. The elements left in the order they came in. On a stack, the same operations would have made the first pop return 5.

5Empty and full queues

Two operations cannot always run. You cannot dequeue or peek on an empty queue, because there is no element to remove or look at. In a fixed-size array you cannot enqueue once there is no room left. So isFull is checked before enqueue, and isEmpty before dequeue and peek.

How this is reported depends on the language. In the C code on this site the function returns false; Java throws IllegalStateException and Python raises IndexError.

6Two ways to build a queue

With an array: the elements sit in an array. front is the index of the front element and rear is the index of the rear element. An empty queue has front at 0 and rear at -1. Details are in Creating an array queue.

30815234frontrear
An array queue: 3, 8 and 5 were added. front = 0 points at 3 at the front, rear = 2 points at 5 at the rear.

The array has a problem: dequeue moves front to the right, and the cells at the start are never used again. The wasted cells lesson shows this. The fix is to wrap around to the start at the end of the array: the circular queue.

With a linked list: each element is a node. front points at the first node and rear at the last one. Enqueue adds a node at the end, dequeue removes the first node. There is no size limit, but each node carries one extra pointer. Details are in Creating a linked-list queue.

NULL3?0x1b208?0x1a905?0x2c10frontrear
The same queue with a linked list: front points at the first node (3), rear at the last node (5). Adding happens at the end, removing at the start.

If you link the last node back to the first, one pointer is enough: the queue with a circular linked list. All implementations offer the same operations under the same names; when to choose which is covered in Array or linked list?.

7Kinds of queue

  • Linear queue: the simplest array queue. It cannot use the cells freed at the start.
  • Circular queue: behaves as if the end of the array were joined to its start; freed cells are used again. See What is a circular queue?.
  • Priority queue: an element leaves by its priority, not by when it arrived. Think of the line at an emergency room. See What is a priority queue?.
  • Deque: a queue where you can add and remove at both ends. See Deque: the double-ended queue.

The and the keep or extend the FIFO order. The is not FIFO; the word "queue" in its name only comes from its operations.

8Where is a queue used?

  • Operating systems: jobs waiting to run sit in a queue. Each job gets a short slice of time in turn. See Round-robin.
  • Printers and file jobs: incoming jobs are done in the order they arrived.
  • Network buffers: data packets arriving at a computer wait in a queue until they are handled.
  • Breadth-first search (BFS): in a graph or a maze, nearby points are visited first; the points waiting their turn are kept in a queue.
  • A queue from two stacks: a queue can also be built from two stacks. See A queue from two stacks.

9What this module covers

10Try it

On an empty queue you run enqueue(4), enqueue(7), dequeue(), enqueue(2). What does peek() return now?

Show the answer

The answer is B. Dequeue removes 4, which came in first. 7 is left, and then 2 joins behind it. The front element is 7. On a stack the answer would be 2.

Which of these does not follow the FIFO order?

Show the answer

The answer is B. Undo reverses the latest change first: that is LIFO and is done with a stack. In the other two, whoever comes first is handled first.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.