1.
PYTHON DATA STRUCTURE OPERATIONS
list=[1,2,3,4,5]
print("Initial list:",list)
[Link](6)
print("After adding 6:",list)
[Link](0,0)
print("After insert 0:",list)
print("element at 4:",list[4])
print ("Last element:",list[-1])
[Link](4)
print("after remove 4:",list)
2. IMPLEMENTATION OF SINGLY LINKED LIST
class Node:
def __init__(self,data):
[Link]=data
[Link]=None
class SinglyLinkedList:
def __init__(self):
[Link]=None
def create(self,data):
new_node=Node(data)
if not [Link]:
[Link]=new_node
print(f"List created with head node:{data}")
return
last_node=[Link]
while last_node.next:
last_node=last_node.next
last_node.next=new_node
print(f"Added node with value{data}")
def add(self,data):
new_node=Node(data)
new_node.next=[Link]
[Link]=new_node
print(f"Added node with value {data}")
def remove(self,data):
if not [Link]:
print("List is Empty")
return
if [Link]==data:
[Link]=[Link]
print(f"Removed element {data} from head")
return
current_node=[Link]
while current_node.next and current_node.[Link]!=data:
current_node=current_node.next
if current_node.next:
current_node.next=current_node.[Link]
print(f"Removed element {data}")
def display(self):
current_node=[Link]
while current_node:
print(current_node.data,end="->")
current_node=current_node.next
print("None")
sll=SinglyLinkedList()
while True:
print("1. Create List")
print("2. Add element")
print("3. Remove Element")
print("4. Display List")
print("5. Exit")
choice=int(input("\n Enter your choice: "))
if choice==1:
data=int(input("Enter the value for the head node: "))
[Link](data)
elif choice==2:
data=int(input("Enter the value add to start: "))
[Link](data)
elif choice==3:
data=int(input("Enter the value to remove: "))
[Link](data)
elif choice==4:
print("current Linked list:",end='')
[Link]()
elif choice==5:
print("Exiting........ ")
break
else:
print("Invalid [Link] try again.")
3. IMPLEMENTATION OF STACK
class stack:
def __init__(self):
[Link]=[]
def push(self,item):
[Link](item)
print(f"{item} pushed to stack.")
def pop(self):
if not [Link]():
ritem=[Link]()
print(f" {ritem} poped from stack.")
return ritem
else:
print("Stack is empty.")
def isempty(self):
return len([Link])==0
def display(self):
if not [Link]():
print("Stack elements from top to bottom:")
for item in reversed([Link]):
print(item)
else:
print("Stack is empty!")
s=stack()
while True:
print("\n Menu")
print("1. Push element to stack")
print("2. Pop element from stack")
print("3. Display stack")
print("4. Exit")
choice=int(input("\nEnter your choice:"))
if choice==1:
item=int(input("Enter the value to push on to the stack:"))
[Link](item)
elif choice==2:
[Link]()
elif choice==3:
[Link]()
elif choice==4:
print("Exiting....")
break
else:
print("Invalid choice. Please select a valid option.")
4. IMPLEMENTATION OF QUEUE
class queue:
def __init__(self):
[Link]=[]
def enqueue(self,item):
[Link](item)
print(f"{item} enqueueed to queue")
def dequeue(self):
if not [Link]():
ritem=[Link](0)
print(f"{ritem} dequeued from queue")
return ritem
else:
print(f"Queue is empty,Cannot dequeue an element")
return none
def isempty(self):
return len([Link])==0
def display(self):
if not [Link]():
print(f"Queue eleements from front to rear:")
for item in [Link]:
print(item,end=" ")
print()
else:
print("Queue is empty!")
q=queue()
while True:
print("\n Menu")
print("[Link] element to queue")
print("[Link] element to queue")
print("[Link] Queue")
print("[Link]")
choice=int(input("\n Enter your choice"))
if choice==1:
item=int(input("\n Enter the value to enqueue:"))
[Link](item)
elif choice==2:
[Link]()
elif choice==3:
[Link]()
elif choice==4:
print("Exiting...")
break
else:
print("In valid [Link] enter valid option")
5. PRE-ORDER TRAVERSAL OF A BINARY TREE
class Tree:
def __init__(self,value):
[Link]=value
[Link]=None
[Link]=None
def insert(root,value):
if root is None:
return Tree(value)
else:
if value<[Link]:
[Link]=insert([Link],value)
else:
[Link]=insert([Link],value)
return root
6. BUBBLE SORT
def bubble_sort(a):
n=len(a)
for i in range(n):
for j in range(0,n-i-1):
if a[j]>a[j+1]:
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
n=int(input("Enter the number of elements in the list : "))
a=[]
for i in range(n):
element=int(input(f"Enter element {i+1} : "))
[Link](element)
print("Original list : ",a)
bubble_sort(a)
print("sorted list : ",a)
7. LINEAR SEARCH
def linear_search(a,x):
for i in range(len(a)):
if a[i]==x:
return i
return -1
n=int(input("Enter the number of elements in the list:"))
l1=[]
for i in range(n):
element=int(input(f"Enter element {i+1}:"))
[Link](element)
value=int(input("Enter the value to search for:"))
pos=linear_search(l1,value)
if pos!=-1:
print(f"Value found at position {pos}.")
else:
print("Value not found.")
8. BINARY SEARCH
def binary_search(a,x):
left=0
right=len(a)-1
while left<=right:
mid=(left+right)//2
midvalue=a[mid]
if midvalue==x:
return mid
elif midvalue<x:
left=mid+1
else:
right=mid-1
return -1
n=int(input("Enter the number of elements in the sorted list:"))
sortlist=[]
for i in range(n):
element=int(input(f"Enter element {i+1}:"))
[Link](element)
x=int(input("Enter the target value to search for: "))
pos=binary_search(sortlist,x)
if pos!=-1:
print(f"Value found at index {pos}.")
else:
print("Value not found")