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 stack works using dynamic linked list implementation. Watch how nodes are created, linked, and managed with flexible memory allocation.
Select an operation to begin visualization
Create new node, link to current head, update head pointer
Store head value, move head to next, deallocate old head
Return head 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 StackLinkedList:
def __init__(self):
self.head = None # Reference to top of stack
self.size = 0 # Track number of elements
def push(self, value):
# Create new node
new_node = Node(value)
# Link new node to current head
new_node.next = self.head
# Update head to point to new node
self.head = new_node
self.size += 1
return value
def pop(self):
# Check for stack underflow
if self.head is None:
raise Exception("Stack Underflow")
# Get value from head node
value = self.head.value
# Update head to next node
self.head = self.head.next
self.size -= 1
# Note: In languages without garbage collection,
# you would need to free the memory of the old head
return value
def peek(self):
# Check if stack is empty
if self.head is None:
raise Exception("Stack is Empty")
return self.head.value
def is_empty(self):
return self.head is None
def get_size(self):
return self.size
def display(self):
# Traverse and display all elements
current = self.head
elements = []
while current:
elements.append(current.value)
current = current.next
return elements