DSA Practical
DSA Practical
def display(self):
temp = self.head
print("Nodes of singly linked list:")
while temp:
print(temp.data, end="\n" if temp.next else " -> None\n")
temp = temp.next
sll = SinglyLinkedList()
sll.insert(1)
sll.insert(2)
sll.insert(3)
sll.insert(4)
sll.display()
4
class DoublyLinkedList:
def __init__(self):
self.head = None
def display(self):
temp = self.head
print("Nodes of doubly linked list:")
while temp:
print(temp.data, end=" <-> " if temp.next else " ->
None\n")
temp = temp.next
dll = DoublyLinkedList()
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.display()
5
class CircularLinkedList:
def __init__(self):
self.head = None
def display(self):
if not self.head:
print("List is empty")
return
temp = self.head
print("Circular Linked List:\n ", end="")
while True:
print(temp.data, end=" -> ")
temp = temp.next
if temp == self.head:
print(f"{self.head.data} (back to head)")
break
cll = CircularLinkedList()
for i in [1, 2, 3, 4]:
cll.insert(i)
cll.display()
6
postfix_expression = "562+*31-/"
result = evaluate_postfix(postfix_expression)
print(f"Result of postfix expression '{postfix_expression}' is
{result}")
7
t = Tree()
for v in [7, 5, 4, 3, 2, 1]:
t.add(v)
print("Constructed Tree:")
t.show(t.root)
8
bst = BST()
for v in [7, 5, 4, 3, 2, 1]:
bst.add(v)
print("Constructed BST:")
bst.show(bst.root)
9
while pq:
curr_dist, node = heapq.heappop(pq)
if curr_dist > dist[node]:
continue
for neighbor, weight in graph[node]:
new_dist = curr_dist + weight
if new_dist < dist[neighbor]:
dist[neighbor] = new_dist
prev[neighbor] = node
heapq.heappush(pq, (new_dist, neighbor))
def path(node):
p = []
while node:
p.insert(0, node)
node = prev[node]
return p
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('C', 2), ('D', 5)],
'C': [('D', 1)],
'D': []
}
def MergeSort(A):
print('Splitting', A)
n = len(A)
if(n > 1):
mid = n // 2
left = A[:mid]
right = A[mid:]
MergeSort(left)
MergeSort(right)
Merge(left, right, A)
MergeSort(A)
print("Sorted List:\n")
print(A)
11
Sorted array is :
def insertionSort(arr): [5, 6, 11, 12, 13]
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
lst = []
print("Sorted array is : ")
for i in range(len(arr)):
lst.append(arr[i])
print(lst)
if found:
print("Element found at index ", mid)
else:
print("Element not found")
ALGORITHMS
1. STACK ADT
Initialize an empty stack.
Push element: Add the element to the stack.
Pop element: Remove and return the top element of the stack.
Peek: Return the top element without removing it.
2. QUEUE ADT
Initialize an empty queue.
Enqueue element: Add the element to the rear of the queue.
Dequeue element: Remove and return the front element of the queue.
Peek: Return the front element without removing it.
Check if the queue is empty.
Display all elements in the queue.
7. BINARY TREE
Initialize an empty tree with a root node.
Insert element: Add a node based on binary tree rules (left or right).
Search element: Recursively search for a node by value.
Traverse tree: Perform in-order, pre-order, or post-order traversal.
Delete element: Remove a node while maintaining binary tree properties.
Display all nodes in a specific order.
9. DIJKSTRA'S ALGORITHM
Initialize a set of visited nodes and a distance map.
Set the initial node distance to 0, and others to infinity.
For the current node, check its neighbors and update their distances.
Mark the current node as visited and move to the next closest unvisited node.
Repeat until all nodes are visited.
Return the shortest distance to each node.
15