Queue Class 12 Notes: FIFO, Operations and Deque

These are complete revision notes for the Queue chapter of the CBSE Class 12 Computer Science syllabus, following the 2026-27 session. The page covers FIFO, enqueue and dequeue, Python list implementation, deque and palindrome checking, and is built for last-minute revision, so it is shorter and tighter than the textbook.

What the Queue Chapter Covers After Stack

The previous chapter introduced Stack, which works on Last-In-First-Out (LIFO): the most recently added item is removed first. The Queue chapter (Chapter 4 of the NCERT Computer Science textbook) builds the exact opposite idea — First-In-First-Out (FIFO): the first item added is the first one removed (NCERT, p. 53).

Linking the two in revision is important: stack and queue are the two basic linear data structures, and deque — introduced later in the same chapter — combines both. The chapter falls into five teaching blocks (NCERT, p. 53):

  • Introduction to Queue
  • Operations on Queue
  • Implementation of Queue using Python
  • Introduction to Deque
  • Implementation of Deque using Python

Queue Basics: FIFO, Front and Rear

A queue is an ordered linear list of elements with different ends for adding and removing elements (NCERT, p. 53). This single design choice makes a queue behave like a line of people: nobody overtakes, and the person who joined first is served first.

A line of people queued at a bank cash counter, illustrating the FIFO behaviour of a queue data structure
Figure 4.1: Queue of people at a bank. Source: NCERT
Cars waiting in line at a petrol pump, another everyday example of a first-in-first-out queue
Figure 4.2: Queue of cars in a petrol pump. Source: NCERT

The whole idea of a queue sits in two words (NCERT, p. 54):

  • REAR (also called TAIL) — the end where new items enter.
  • FRONT (also called HEAD) — the end from which items leave.

Because elements leave in the order they entered, a queue follows the First In First Out (FIFO) principle, also called a First Come First Served (FCFS) approach. The element that has stayed longest in the queue is removed first (NCERT, p. 54).

Everyday examples of queue lines include students lining up for morning assembly, customers at a bank cash counter, and vehicles at a fuel pump (NCERT, p. 53). Three more vivid examples from the textbook (NCERT, pp. 54–55):

  • If a train ticket is in the waiting list, say W/L1, it sits in a queue of waiting tickets. When a confirmed ticket is cancelled, the W/L1 ticket is removed from the FRONT of the waiting queue and confirmed.
  • On a single-lane one-way road, the vehicle that entered first must exit first — you cannot overtake.
  • At a highway toll booth, vehicles are served following the FIFO order in which they arrive.

Computer science uses the same idea in three important places (NCERT, p. 55):

  • A web server hosting a result website can handle only a limited number of concurrent requests (say 50), so thousands of user requests are placed in a queue and served in order.
  • In a multitasking operating system, jobs that seek the processor are queued and given processor access on a FIFO basis — in the order they arrived.
  • For a shared printer, the OS queues print commands from multiple files/computers and sends them to the printer one by one.

Queue Terminology: Terms You Must Know

Most direct questions in this chapter ask for definitions, so learn these terms exactly. Each one is used in the same meanings throughout the chapter.

Term Meaning Example
Queue An ordered linear list in which insertion and deletion happen at different ends (NCERT, p. 53) People standing in a line at a bank counter
FIFO First In First Out — the element entering first leaves first (NCERT, p. 54) The first person in line is served first
FCFS First Come First Served — another name for FIFO (NCERT, p. 54) Vehicles at a toll booth
Front / Head The end from which elements are removed (NCERT, p. 54) The person at the front of the queue is dequeued next
Rear / Tail The end at which new elements are added (NCERT, p. 54) A new person joins the line at the rear
Enqueue Operation to insert a new element at the rear of the queue (NCERT, p. 55) enqueue(q, 'A1')
Dequeue Operation to remove one element from the front of the queue (NCERT, p. 55) dequeue(q)
Overflow Trying to insert into a queue that is already full (NCERT, p. 55) Adding an element beyond the queue’s capacity
Underflow Trying to delete from an empty queue (NCERT, p. 55) Dequeueing when no element is left
isEmpty Checks whether the queue has any element, to avoid underflow (NCERT, p. 55) Called before every dequeue
isFull Checks whether more elements can be added, to avoid overflow (NCERT, p. 56) Not needed in Python (see below)
Peek Views the element at the front without removing it (NCERT, p. 56) q[0] returns the front value

Queue Operations: Enqueue, Dequeue, Peek, isEmpty, isFull

Every queue implementation reduces to these five operations. Notice which end each one touches — that is the most common point of confusion.

  • ENQUEUE — inserts a new element at the rear end. Insertion is possible only while space remains; inserting beyond capacity causes Overflow (NCERT, p. 55).
  • DEQUEUE — removes one element from the front. Deletion is possible only while the queue is not empty; deleting from an empty queue causes Underflow (NCERT, p. 55).
  • IS EMPTY — used before dequeue to check whether the queue has any element, protecting against underflow (NCERT, p. 55).
  • PEEK — views the element at the front without removing it (NCERT, p. 56).
  • IS FULL — used before enqueue to check whether more elements can be added, protecting against overflow (NCERT, p. 56).

Why the guards matter: overflow is an exception caused by inserting beyond capacity, while underflow is an exception caused by deleting from an empty queue (NCERT, p. 55).

The textbook’s Think and Reflect prompt on page 55 asks how an urgent administrator request could be served ahead of student requests — that is a discussion of priority, not a queue rule, so keep it as a discussion point only.

Queue Rules to Remember: This Chapter’s Formula Box

This chapter has no mathematical formulas, but it has invariants that play the same role. Treat the following rules like formulas — they are the lines to reproduce in any answer:

Rule Why it holds Python form
FIFO always The element longest in the queue leaves first (NCERT, p. 54) —
Insertion only at rear New items join the queue at the REAR end (NCERT, p. 54) append()
Deletion only at front Items leave from the FRONT end (NCERT, p. 54) pop(0)
Element count Number of elements currently in the queue len()
Peek front Read the front value without removing it queue[0]
Deque opens both ends Insertion and deletion are allowed at either end (NCERT, p. 59) insert(), append(), pop(), pop(0)

Implementing Queue in Python Using a List

A Python list is the natural home of a queue because lists are dynamic and already provide end operations. The implementation needs six pieces (NCERT, pp. 56–58):

q = list()                     # empty queue

def enqueue(q, element):       # insert at REAR
    q.append(element)          # append() always adds at the end

def isEmpty(q):                # guard against underflow
    return len(q) == 0

def dequeue(q):                # remove from FRONT
    if not isEmpty(q):
        return q.pop(0)        # pop(0) deletes from the beginning
    else:
        print("Queue is empty")

def size(q):                   # number of elements
    return len(q)

def peek(q):                   # read front without removing
    if isEmpty(q):
        print('Queue is empty')
        return None
    else:
        return q[0]

Three decisions in this code deserve attention:

  • append() always adds at the end of the list, so it inserts at the REAR of the queue (NCERT, p. 56).
  • pop(0) deletes from the beginning of the list, so it removes from the FRONT of the queue (NCERT, p. 57).
  • Python lists grow dynamically, so there is no fixed capacity and no need for an isFull check — overflow never happens with a list-based queue (NCERT, pp. 56–57).

Two small notes for revision: the functions are user-defined, so their names can differ (NCERT, p. 57), and the textbook’s Think and Reflect prompt asks whether a tuple or dictionary could implement a queue — worth a thought, but not a required answer.

Worked Example: Tracing a Queue Step by Step

Method: FIFO trace using Python list functions — after every operation, rewrite the queue from Front to Rear and note what enqueue, peek or dequeue returned.

Start with an empty queue q and follow this sequence: enqueue('A1'), enqueue('B2'), peek(), dequeue(), enqueue('C3'), dequeue(), dequeue(), dequeue().

Operation Queue status (Front → Rear) What happens
Initial empty Create empty queue
enqueue(‘A1’) A1 A1 added at rear
enqueue(‘B2’) A1 → B2 B2 added at rear
peek() A1 → B2 Returns A1, queue unchanged
dequeue() B2 Returns A1 (front), removed
enqueue(‘C3’) B2 → C3 C3 added at rear
dequeue() C3 Returns B2 (front), removed
dequeue() empty Returns C3 (front), removed
dequeue() empty isEmpty check fails → prints “Queue is empty” (underflow)

Notice the order of outputs: A1, B2, C3 — exactly the insertion order. That is FIFO in action. The final dequeue demonstrates the underflow exception: the function checks isEmpty() first and prints the message instead of crashing (NCERT, p. 57).

Deque: Inserting and Deleting at Both Ends

A deque (pronounced “deck”) is an ordered linear list that permits addition and removal of elements from any end — front/head or rear/tail (NCERT, p. 59).

Because it applies no restriction on which end is used, it is also called a double-ended queue, and it can be used to implement either a stack or a queue in a program (NCERT, p. 59).

The four main operations and the end each one touches:

Operation End touched Behaviour
INSERTFRONT Front Insert a new element at the front of the deque (NCERT, p. 60)
INSERTREAR Rear Same as normal queue enqueue — insert at the rear (NCERT, p. 60)
DELETIONFRONT Front Same as normal queue dequeue — remove from the front (NCERT, p. 60)
DELETIONREAR Rear Remove one element at a time from the rear (NCERT, p. 60)

Deque applications in real life (NCERT, pp. 59–60):

  • At a train ticket counter, a person who already bought a ticket and returns to ask a question may join the queue again from the front as a privilege.
  • At a toll gate with parallel booths, when one booth’s queue empties, vehicles from other queues may leave from the rear of their current booth and join the vacant booth’s queue in front.

Deque applications in computer science (NCERT, p. 60):

  • Browser history (URLs): a stack opens the most recently closed URL first (Ctrl+Shift+T), but when the fixed-size history list grows too large, the least-visited URL from the end of the list gets deleted — deletion from the opposite end makes it a deque.
  • Undo/Redo in text editors: same mechanism — operate from one end normally, trim from the other when the list is full.
  • Palindrome checking: process a string left to right, then compare characters taken from both ends (NCERT, p. 60).

The two activities on page 60 capture the most important insight about deque:

  • If insertion and deletion happen from the same end, the deque behaves like a stack (LIFO).
  • If insertion and deletion happen from opposite ends, the deque behaves like a queue (FIFO).

Palindrome Check with a Deque (Algorithm 4.1)

A palindrome is a string that reads the same forwards and backwards, such as “radar” or “civic”. Algorithm 4.1 (NCERT, p. 61) checks this using a deque in two phases:

  1. Traverse the string from the left, one character at a time.
  2. Insert each character into the deque using INSERTREAR, as in a normal queue.
  3. Repeat for all characters of the string.
  4. Now remove one character from the front (DELETIONFRONT) and one from the rear (DELETIONREAR).
  5. Match the two removed characters.
  6. If they are the same, repeat steps 4 and 5 until the deque is empty or left with only one character — then the string is a palindrome. If any pair mismatches, stop: the string is not a palindrome.

The textbook’s Figures 4.5 and 4.6 illustrate the deque status during the fourth iteration and after removing one character from both ends. Working through an original example, “civic”:

Round Deque before comparison (Front → Rear) Deleted from front Deleted from rear Match?
Insert all c → i → v → i → c — — —
1 i → v → i c c Yes
2 v i i Yes
3 v (after Round 2 only ‘v’ remains, so the loop stops) — — Single character left → palindrome

The stopping condition is the part students forget: the string is a palindrome when the deque becomes empty or has exactly one character left. A single leftover middle character (the “v” in “civic”) is a success, not a failure (NCERT, p. 60).

Implementing Deque in Python: Code for Both Ends

Deque uses the same Python list, but now both ends are active. The core functions (NCERT, pp. 62–63), written here with a deque named dq:

dq = list()                      # empty deque

def insertFront(dq, element):    # insert at FRONT
    dq.insert(0, element)        # insert() with index 0 puts data at front

def insertRear(dq, element):     # insert at REAR
    dq.append(element)           # same as enqueue of normal queue

def isEmpty(dq):
    return len(dq) == 0

def deletionFront(dq):           # delete from FRONT
    if isEmpty(dq):
        print("Queue underflow")
    else:
        return dq.pop(0)         # pop(0) deletes first element

def deletionRear(dq):            # delete from REAR
    if not isEmpty(dq):
        return dq.pop()          # pop() deletes last element
    else:
        print("Deque empty")

def getFront(dq):                # read front, don't delete
    if not isEmpty(dq):
        return dq[0]
    else:
        print("Queue empty")

def getRear(dq):                 # read rear, don't delete
    if not isEmpty(dq):
        return dq[len(dq)-1]
    else:
        print("Deque empty")

The method map is the whole chapter in four lines:

  • insert(0, element) puts data at the front.
  • append() puts data at the rear.
  • pop(0) deletes from the front.
  • pop() deletes from the rear.

getFront() and getRear() return a copy of the value — they read without removing (NCERT, p. 62). The textbook’s main() (Program 4-2) is menu-driven: the user types 1 to use the deque as a normal queue (insertRear, deletionFront) or 2 for the other mode (insertFront, deletionRear).

Its sample run shows that after the queue empties, further deletion attempts print “Queue underflow” and return None (NCERT, p. 64).

Common Mistakes Students Make with Queue and Deque

Every error below is one this chapter genuinely produces. Fix the habit, and the code will follow FIFO correctly.

Mistake Correct rule How to check your answer
Writing dequeue with pop() Use pop(0) — pop() removes the rear element and breaks FIFO (NCERT, p. 57) Trace the first two operations: the first-inserted element must come out first
Dequeueing an empty queue without checking Guard with isEmpty() first — deletion from an empty queue is underflow (NCERT, p. 55) Run dequeue twice on a one-element queue; the second call must print an error message
Confusing deque with dequeue Deque is the double-ended data structure; dequeue is the delete operation (NCERT, pp. 55, 59) Say the sentence: “deque is a structure, dequeue is an action”
Looking for an isFull check in Python Python lists are dynamic, so no fixed capacity means no overflow and no isFull needed (NCERT, pp. 56–57) Append repeatedly — the list never fills up
Calling isEmpty() without passing the deque name Write isEmpty(myDeque) so the function checks the right deque (NCERT, p. 62) Type-check the call: every parameter must be passed
Treating a single leftover character in the palindrome loop as failure 0 or 1 characters left means the string is a palindrome (NCERT, p. 60) Test “civic”: the middle “v” remains, but the string is still a palindrome

How to Present Queue Answers in Exams

The chapter’s exercises (NCERT, pp. 65–66) come in five shapes. Match the presentation to the shape:

  • Fill-in-the-blank questions (e.g. “Insertion operation in a queue is called ______”) test vocabulary. Write the exact operation name — enqueue, dequeue, front, rear, deque — because these are standard terms (NCERT, p. 65).
  • Compare questions (queue vs stack, queue vs deque) are best answered as a table. Rows to include: working principle, insertion end, deletion end, and Python methods used for insertion and deletion. A table shows the contrast in seconds.
  • Trace questions (“Show the status of queue after each operation”) need a table with one row per operation. Write the queue status as Front → Rear after each step so the order is unmistakable, and mark what enqueue, dequeue or peek returned (NCERT, p. 66).
  • Program questions (menu-driven programs) should name each function first, then give clean commented code, then a sample run. The textbook’s shuttlecock and deque programs are of this type — follow the same structure as the functions above.
  • The palindrome program must follow Algorithm 4.1 step by step: insert all characters at rear, then repeatedly compare one front character with one rear character until the deque is empty or has one element (NCERT, p. 61). State the stopping condition explicitly in your answer.

Queue and Deque: One-Screen Revision Recap

If you remember only one table, remember this one — it maps the three data structures to the exact Python methods:

Feature Stack (LIFO) Queue (FIFO) Deque
Principle Last In First Out First In First Out (NCERT, p. 54) No restriction — either end (NCERT, p. 59)
Insertion end Top (same end) Rear Front or rear
Deletion end Top (same end) Front Front or rear
Python insertion append() append() insert(0, e) or append()
Python deletion pop() pop(0) pop(0) or pop()
Typical use Undo in editors Printer queue, job scheduling Browser history, palindrome check

The chapter’s own summary points, restated (NCERT, pp. 64–65):

  • A queue is an ordered linear data structure following the FIFO strategy.
  • Front and Rear indicate the beginning and end of the queue.
  • Insertion (enqueue) happens at the rear; deletion (dequeue) happens at the front.
  • Supporting operations are isEmpty, isFull and peek.
  • A deque allows insertion and deletion at both ends, so it supports both stack and queue behaviour.
  • Other deque operations: insertFront, insertRear, deletionFront, deletionRear, getFront, getRear, isEmpty, isFull.

Memory device: “Enter at REAR, exit at FRONT — first in, first out.” Say it once while tracing a queue, and you will never write pop() for a dequeue again.

Queue Class 12 — Frequently Asked Questions

Why is a queue called FIFO when a stack is called LIFO?

Because of where elements leave. In a queue, the element that entered first leaves first (First In First Out), while in a stack the most recently added element leaves first (Last In First Out). The names describe the exit order, not the storage (NCERT, pp. 53–54).

What is the difference between deque and dequeue?

They are different words. Deque (pronounced “deck”) is the data structure that allows insertion and deletion at both ends (NCERT, p. 59). Dequeue is the queue operation that removes an element from the front (NCERT, p. 55). One is a structure, the other is an action.

Why does dequeue use pop(0) and not pop()?

Because pop(0) removes the element at index 0, which is the FRONT of the queue, while pop() removes the last element, which is the REAR. Using pop() would delete the most recently added element and break the FIFO rule (NCERT, p. 57).

Can a deque be used as both a stack and a queue?

Yes. If insertion and deletion happen from the same end, the deque behaves like a stack. If they happen from opposite ends, it behaves like a queue (NCERT, p. 60).

What happens when we dequeue from an empty queue?

That is an underflow situation. A well-written dequeue function checks isEmpty() first and prints a message such as “Queue is empty” instead of attempting the deletion (NCERT, pp. 55, 57).

How should I show the state of a queue after each operation in an answer?

Use a table with one row per operation. In each row, write the operation, then the queue status as Front → Rear after the operation, and what value (if any) was returned. This is the format the textbook itself uses for its queue figure (NCERT, p. 56).

Reference: NCERT Class 12 Computer Science textbook, chapter Queue.

Explore Class 12 Computer Science Notes

  • Previous: File Handling in Python
  • Next: Sorting

Class 12 Computer Science on LearnCBSE:

Related chapters:

  • Searching notes
  • Understanding Data notes
  • Database Concepts notes


Official source: download the NCERT textbook free from ncert.nic.in.

Related

More from this section