DSAverse
Initializing Sorting Algorithms...
Sorting Algorithm
Binary Tree
Graph Traversal
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Explore how a queue works using dynamic linked list implementation. Watch how nodes are managed with front and rear pointers for efficient FIFO operations.
Select an operation to begin visualization
Create node, link to rear, update rear pointer
Store front value, move front pointer, deallocate
Return front node value without modification
class Node:
def __init__(self, value):
self.value = value # Data stored in the node
self.next = None # Reference to next node
class QueueLinkedList:
def __init__(self):
self.front = None # Reference to front of queue
self.rear = None # Reference to rear of queue
self.size = 0 # Track number of elements
def enqueue(self, value):
# Create new node
new_node = Node(value)
if self.rear is None:
# First node - both front and rear point to it
self.front = self.rear = new_node
else:
# Link new node to current rear
self.rear.next = new_node
# Update rear to point to new node
self.rear = new_node
self.size += 1
return value
def dequeue(self):
# Check for queue underflow
if self.front is None:
raise Exception("Queue Underflow")
# Get value from front node
value = self.front.value
# Update front to next node
self.front = self.front.next
# If queue becomes empty, reset rear to None
if self.front is None:
self.rear = None
self.size -= 1
return value
def peek(self):
# Check if queue is empty
if self.front is None:
raise Exception("Queue is Empty")
return self.front.value
def is_empty(self):
return self.front is None
def get_size(self):
return self.size
def display(self):
# Traverse and display all elements
current = self.front
elements = []
while current:
elements.append(current.value)
current = current.next
return elements