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 ).
Technical Profile: Queue Data Structure
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 |
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.
# 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.
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.
Like queues, deques are implemented in Python using lists, leveraging specific index operations to manipulate both ends of the list structure.
# 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