Ch 4 covers the queue data structure — a linear, FIFO (First In, First Out) collection. Learn enqueue, dequeue operations, Python list implementation, and practical applications.
Queue: FIFO — first element added is the first removed. Think: queue at a ticket counter. Two ends: front (removal) and rear (insertion). Operations: enqueue(item) — add to rear, dequeue() — remove from front, front() — view front, rear() — view rear, isEmpty() — True if empty, size() — count. Underflow: dequeue from empty queue. Comparison with stack: stack = LIFO (one end), queue = FIFO (two ends).
Using Python list: queue = []; queue.append(item) for enqueue; queue.pop(0) for dequeue (note: O(n) — pop(0) shifts all elements). Better: collections.deque — O(1) for both ends. Applications: print job scheduling (first document sent prints first), CPU task scheduling, BFS (Breadth-First Search) in graphs, customer service systems, message queues in distributed systems. Variations: circular queue (rear wraps around), priority queue (highest priority first), double-ended queue (deque — add/remove from both ends).
Download: https://ncert.nic.in/textbook/pdf/lecs104.pdf | Complete Book: https://ncert.nic.in/textbook/pdf/lecs1ps.zip
Python list.pop(0) removes the first element but then must shift ALL remaining elements one position left — O(n) time complexity. For a queue with 1 million elements, every dequeue moves ~1 million items. Solution: collections.deque (double-ended queue) implemented as a doubly-linked list — O(1) for append and popleft. For production code: from collections import deque; q = deque(); q.append(item); q.popleft(). Python's list is optimised for operations at the end (append, pop), not the front.
Book a Trial + Diagnostic session. Get a personalized Learning Path with clear milestones, tutor match, and a plan recommendation — all within 24 hours.
Book Trial + Diagnostic →