PUTHON DSA

Data Structures
Data
Structures are a way of storing and organizing data in a computer.
Python
has built-in support for several data structures, such as lists, dictionaries,
and sets.
Other
data structures can be implemented using Python classes and objects, such as
linked lists, stacks, queues, trees, and graphs.
In
this tutorial we will concentrate on these Data Structures:
Stacks with Python
Stacks
A
stack is a data structure that can hold many elements, and the last element
added is the first one to be removed.
Like
a pile of pancakes, the pancakes are both added and removed from the top. So
when removing a pancake, it will always be the last pancake you added. This way
of organizing elements is called LIFO: Last In First Out.
Basic
operations we can do on a stack are:
- Push: Adds
a new element on the stack.
- Pop: Removes
and returns the top element from the stack.
- Peek: Returns
the top (last) element on the stack.
- isEmpty: Checks
if the stack is empty.
- Size: Finds
the number of elements in the stack.
stack = []
# Push
stack.append('A')
stack.append('B')
stack.append('C')
print("Stack:
", stack)
# Peek
topElement = stack[-1]
print("Peek:
", topElement)
# Pop
poppedElement =
stack.pop()
print("Pop: ",
poppedElement)
# Stack after Pop
print("Stack after
Pop: ", stack)
# isEmpty
isEmpty = not
bool(stack)
print("isEmpty:
", isEmpty)
# Size
print("Size:
",len(stack))

Queues
Think
of a queue as people standing in line in a supermarket.
The
first person to stand in line is also the first who can pay and leave the
supermarket.
Basic
operations we can do on a queue are:
- Enqueue: Adds
a new element to the queue.
- Dequeue: Removes
and returns the first (front) element from the queue.
- Peek: Returns
the first element in the queue.
- isEmpty: Checks
if the queue is empty.
- Size: Finds
the number of elements in the queue.
Queues
can be implemented by using arrays or linked lists.
queue = []
# Enqueue
queue.append('A')
queue.append('B')
queue.append('C')
print("Queue: ", queue)
# Peek
frontElement = queue[0]
print("Peek: ", frontElement)
# Dequeue
poppedElement = queue.pop(0)
print("Dequeue: ", poppedElement)
print("Queue after Dequeue: ", queue)
# isEmpty
isEmpty = not bool(queue)
print("isEmpty: ", isEmpty)
# Size
print("Size: ", len(queue))

Linked Lists with Python
Linked Lists
A
linked list consists of nodes with some sort of data, and a pointer, or link,
to the next node.
![]()

Linked List Operations
Basic
things we can do with linked lists are:
- Traversal
- Remove a node
- Insert a node
- Sort
# Simple Linked List without Class and Object
# Create linked list
linked_list = [30, 10, 50, 20, 40]
# 1. Traversal
print("Original Linked List:")
for data in linked_list:
print(data, end=" -> ")
print("None")
# 2. Insert a Node
# Insert 25 at position 2
linked_list.insert(2, 25)
print("\nAfter Inserting 25:")
for data in linked_list:
print(data, end=" -> ")
print("None")
# 3. Remove a Node
# Remove 50
linked_list.remove(50)
print("\nAfter Removing 50:")
for data in linked_list:
print(data, end=" -> ")
print("None")
# 4. Sort the Linked
List
linked_list.sort()
print("\nAfter Sorting:")
for data in linked_list:
print(data, end=" -> ")
print("None")

Hash Table
A
Hash Table is a data structure designed to be fast to work with.
The
reason Hash Tables are sometimes preferred instead of arrays or linked lists is
because searching for, adding, and deleting data can be done really quickly,
even for large amounts of data.
# Hash Table using Python
# Without Class and Object
# Create an empty
hash table
hash_table = {}
# 1. Insert data
hash_table[101] = "Rahul"
hash_table[102] = "Amit"
hash_table[103] = "Priya"
hash_table[104] = "Neha"
print("Hash Table:")
print(hash_table)
# 2. Search data
key = 102
if key in hash_table:
print("\nSearch Result:")
print("Key:", key)
print("Value:", hash_table[key])
else:
print("Key not found")
# 3. Update data
hash_table[102] = "Amit Kumar"
print("\nAfter Updating Key 102:")
print(hash_table)
# 4. Delete data
del hash_table[103]
print("\nAfter Deleting Key 103:")
print(hash_table)
# 5. Traversal
print("\nTraverse Hash Table:")
for key, value in hash_table.items():
print(key, "->", value)

Comments
Post a Comment