algoritmauzmanı
Sign in
TREN
5.3Queue with a Linked List
Lesson 5.3.4

Peek: looking at the front

looks at the element at the front of the queue but does not remove it. In a queue built on a linked list this is a single read: already points to the front node. In this lesson you see why peek never changes the queue and what happens when the queue is empty.

What you will learn
  • How the front value is read through front
  • Why peek does not change the queue
  • How peek behaves on an empty queue
4 minTime O(1)Memory O(1)C · Java · Python

1Idea

Peek does two things. First it checks whether the queue is empty: if front is NULL there is no node to read. The C code returns false, while Java and Python throw the same error as . If the queue is not empty, front->data is read.

No pointer is written. front, rear and the next fields of the nodes stay the same. So however many times you call peek in a row, you get the same value. If you want the value and also want to remove the element, you use dequeue.

Peek is used to decide about an element before removing it. For example, in a printer queue you can look at the size of the front job and check whether the printer is ready. If it is, you take the job with dequeue; if not, the job keeps its place in the queue.

2Step by step

The drawing runs peek on a queue holding 10, 20 and 30. The front is on the left, the rear on the right. At each step the matching line of code lights up.

NULL10?0x1a4020?0x1a9030?0x2c10frontrear
  1. Start. front points to 10 and rear to 30.
  2. front is not NULL, so the queue is not empty.
  3. The value of the front node is read: 10.
  4. Result: the queue is unchanged. Peek only read; it changed no pointer.

struct Node {    int data;    struct Node* next;}; struct Queue {    struct Node* front;    struct Node* rear;}; bool peek(struct Queue* q, int* value) {    if (q->front == NULL) return false;  // empty queue    *value = q->front->data;   // read, do not remove    return true;}
class Node {    int data;    Node next;    Node(int data) { this.data = data; }} class LinkedQueue {    Node front = null;    Node rear = null;     int peek() {        if (front == null)            throw new IllegalStateException("queue is empty");        return front.data;     // read, do not remove    }}
class Node:    def __init__(self, data):        self.data = data        self.next = None class LinkedQueue:    def __init__(self):        self.front = None        self.rear = None     def peek(self):        if self.front is None:            raise IndexError("queue is empty")        return self.front.data   # read, do not remove

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

One check and one read. The length of the queue does not matter.

Memory
O(1)

No extra room is needed.

Peek is in an array queue too: there the front element is in the cell at index front. You can see that version in peek with an array.

4Common mistake

The most common mistake is skipping the empty-queue check:

*value = q->front->data;   // front may be NULL

If the queue is empty, q->front is NULL and reading q->front->data crashes the program. In Java you get a NullPointerException. That message does not explain the problem; a clear error such as "queue is empty" tells the caller what to do.

5Try it

The queue holds 4, 8, 15 (4 at the front). After peek, peek, dequeue, peek in a row, what does the last peek return?

Show the answer

The answer is B. The two peeks do not change the queue; both return 4. Dequeue removes 4. The last peek returns the new front element, 8.

Why does peek never use rear?

Show the answer

The answer is B. A queue removes from the front and adds at the back. Peek reads the front, so front is enough. rear is what makes fast.

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