Computer Science: Class 12 — Chapter 4: Queue

4.1 Introduction to Queue

A Queue is an ordered linear data structure operating on the First-In-First-Out (FIFO) principle, where the element that has been in the queue the longest is the first to be removed. This is also referred to as the First Come First Served (FCFS) approach. It features two distinct ends: elements are added at the REAR (also known as TAIL ) and removed from the FRONT (also known as HEAD ).

Real-Life Applications of Queues
  • Train Ticket Waiting List: Tickets are placed in a queue; when a confirmed ticket is cancelled, the ticket at the FRONT of the waiting list (W/L1) is confirmed and removed.
  • Interactive Voice Response System (IVRS): Customer service calls are placed in a queue to wait for the next available support representative.
  • Single-Lane Roads & Toll Booths: Vehicles are served and exit in the exact order they entered, adhering to FIFO.
Computer Science Applications of Queues
  • Web Servers: Used to manage concurrent user requests (e.g., a server hosting exam results with a maximum capacity of 50 concurrent requests queues excess traffic).
  • Operating System Job Scheduling: In multitasking systems, jobs seeking CPU execution are queued and processed on a FIFO basis.
  • Print Spooling: Multiple print commands sent to a shared printer are queued and printed sequentially.

Technical Profile: Queue Data Structure

  • Architecture Layer / Tier: Linear Data Structure / System Resource Management Tier
  • Operational Mechanics & Syntax: Restricts insertion to the Rear end and deletion to the Front end. Implemented in Python using list methods.
  • System Trade-offs & Failures: Python list-based queues suffer from $O(n)$ time complexity during deletion ( pop(0) ) due to element shifting. Memory exhaustion can occur if queue size is unbounded.
4.2 Operations on Queue

A standard queue supports several fundamental operations to manipulate and query its state:

Core Computer Science Concept Technical Definition / Operational State Structural Parameters & Specifications Computational Function & System Purpose
Enqueue Insertion operation Adds element to the REAR end Appends data to the end of the processing line
Dequeue Deletion operation Removes element from the FRONT end Retrieves and processes the oldest active element
Peek Inspection operation Reads element at index [0] Allows read-only access to the next-in-line element
Underflow Error state Triggered when Dequeue is called on an empty queue Prevents illegal memory access operations
4.3 Implementation of Queue using Python

In Python, a queue can be dynamically implemented using the built-in list data type. Because Python lists are dynamic, the IS FULL operation is typically not required as memory is allocated dynamically.

Core Implementation Functions
  
    # Initialize an empty queue
    myQueue = list()
    # Enqueue operation
    def enqueue(myQueue, element):
    myQueue.append(element)
    # IsEmpty operation
    def isEmpty(myQueue):
    if len(myQueue) == 0:
    return True
    else:
    return False
    # Dequeue operation
    def dequeue(myQueue):
    if not isEmpty(myQueue):
    return myQueue.pop(0)
    else:
    print("Queue is empty")
    # Size operation
    def size(myQueue):
    return len(myQueue)
    # Peek operation
    def peek(myQueue):
    if isEmpty(myQueue):
    print("Queue is empty")
    return None
    else:
    return myQueue[0]
  

Case Study: Bank Cash Counter Simulation (Program 4-1)


Socio-Technical Analysis & Core Systemic Impact: This program simulates a real-world banking queue where customers arrive (enqueue) and are served by a cashier (dequeue). The system tracks queue size and handles underflow gracefully when no customers remain. It demonstrates how software models physical human queues to optimize service delivery and resource allocation.

4.4 Introduction to Deque

A Deque (pronounced "deck"), or Double-Ended Queue, is a linear data structure that permits insertion and deletion of elements from both ends ( FRONT and REAR ). It does not impose FIFO or LIFO restrictions, allowing it to behave as either a stack or a queue depending on the operations used.

Applications of Deque
  • Toll Booth Queue Balancing: When a toll booth queue clears, vehicles from the rear of adjacent queues can exit their current queue and join the empty lane from the front.
  • Browser History Management: Tracks visited URLs. New URLs are added to the stack. When the history limit is reached, the oldest URLs are deleted from the opposite end.
  • Undo/Redo Operations: Text editors use double-ended structures to manage edit history and support undo/redo states.
  • Palindrome Checking: Characters of a string are inserted into a deque. Elements are repeatedly removed from both ends and compared; if all pairs match, the string is a palindrome.
Operations on Deque
  • INSERTFRONT : Inserts an element at the front of the deque.
  • INSERTREAR : Inserts an element at the rear of the deque (identical to standard enqueue).
  • DELETIONFRONT : Removes an element from the front of the deque (identical to standard dequeue).
  • DELETIONREAR : Removes an element from the rear of the deque.
4.5 Implementation of Deque Using Python

Like queues, deques are implemented in Python using lists, leveraging specific index operations to manipulate both ends of the list structure.

Core Deque Functions
  
    # Initialize Deque
    myDeque = list()
    # Insert at Front
    def insertFront(myDeque, element):
    myDeque.insert(0, element)
    # Insert at Rear
    def insertRear(myDeque, element):
    myDeque.append(element)
    # Delete from Rear
    def deletionRear(myDeque):
    if not isEmpty(myDeque):
    return myDeque.pop()
    else:
    print("Queue underflow")
    # Delete from Front
    def deletionFront(myDeque):
    if not isEmpty(myDeque):
    return myDeque.pop(0)
    else:
    print("Queue underflow")
    # Get Front Element
    def getFront(myDeque):
    if not isEmpty(myDeque):
    return myDeque[0]
    else:
    print("Queue underflow")
    # Get Rear Element
    def getRear(myDeque):
    if not isEmpty(myDeque):
    return myDeque[len(myDeque)-1]
    else:
    print("Queue underflow")
    # Check if Empty
    def isEmpty(myDeque):
    return len(myDeque) == 0