Lab Programs 3
Lab Programs 3
PROGRAM 4: Write a menu driven program to perform the following operations on a linear list
accept elements, search for an element, traverse the list, modify an element and delete the first
occurrence of the element in the list.
lst=[]
def newlist():
global lst
print("Enter the list items with space between them :\t")
lst=input()
lst=lst.split(" ")
def add():
global lst
print("Enter the element to be added to the list: \t")
ele=input()
lst.append(ele)
def traversal():
global lst
for i in lst:
print(i,end=' ')
def linearsearch():
global lst
ele=input("Enter the element to be searched: ")
index=0
for i in lst:
if i==ele:
print(ele,' found at index value ', index)
break
index +=1
else:
print("element not found")
def modify():
global lst
ele=input("Enter the element to be modified: ")
for i in range(len(lst)):
if ele==lst[i]:
print("Element found. Enter new value: \t")
item=input()
lst[i]=item
print("List modified")
break
else:
print("Element not found")
def deleteitem():
global lst
c=0
ele=input("Enter the element to be deleted: ")
for i in lst:
if i==ele:
lst.remove(ele)
c=1
break
if c==0:
print("Element not found")
elif c==1:
print("Element deleted successfully")
def menu():
print("1. Linear search \n2. Traverse \n3. Modify \n4. Add element to the list \n5. Delete an element
from the list \n")
choice=int(input("Enter your choice: "))
print()
if choice==1:
linearsearch()
print()
elif choice==2:
traversal()
print()
elif choice==3:
modify()
print()
elif choice==4:
add()
print()
elif choice==5:
deleteitem()
print()
else:
print("Invalid choice")
ch='y'
newlist() while
ch=='y':
menu()
ch=input("Do you wish to continue:(y/n) ")
OUTPUT:
11 22 33 55 10
Do you wish to continue:(y/n) y
1. Linear search
2. Traverse
3. Modify
4. Add element to the list
5. Delete an element from the list
11 22 99 55 10 15
Do you wish to continue:(y/n) y
1. Linear search
2. Traverse
3. Modify
4. Add element to the list
5. Delete an element from the list
11 22 55 10 15
Do you wish to continue:(y/n) n
DATA STRUCTURES
PROGRAM 5: Write a menu based program to add, delete and display the record of hostel using
list as stack data structure in python. Record of hostel contains the fields : Hostel number, Total
Students and Total Rooms
#Display Records
def display(host):
if isEmpty(host):
print("Stack is empty")
else:
top=len(host)
print("Hostel No \tNo. of Students \tTotal Rooms")
for i in range(top-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
#Driver code
host=[]
top=None
ch='y'
while ch=='y':
print("1. Add record \n2. Delete Record \n3.Display\n")
choice=int(input("Enter your choice: "))
if choice==1:
push(host)
elif choice==2:
pop(host)
elif choice==3:
display(host)
else:
print("Invalid choice")
ch=input("Do you wish to continue:(y/n) ")
OUTPUT:
1. Add record
2. Delete Record
3.Display
13 40 20
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display
PROGRAM 6: Write a menu based program to add, delete and display the record of hostel using
list as queue data structure in python. Record of hostel contains the fields : Hostel number, Total
Students and Total Rooms
#Display Records
def display(host):
if isEmpty(host):
print("Queue is empty")
elif len(host)==1:
print(host[0],"<--front,rear")
else:
front=0
rear=len(host)-1
print("[Hostel No,No. of Students,Total Rooms]")
print(host[front],"<--front")
for i in range(1,rear):
print(host[i])
print(host[rear],"<--rear")
#Driver code
host=[]
front=rear=None
ch='y'
while ch=='y':
print("1. Add record \n2. Delete Record \n3.Display\n")
choice=int(input("Enter your choice: "))
if choice==1:
enqueue(host)
elif choice==2:
dequeue(host)
elif choice==3:
display(host)
else:
print("Invalid choice")
ch=input("Do you wish to continue:(y/n) ")
OUTPUT:
1. Add record
2. Delete Record
3.Display
11 50 25
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display
def reverseLine(string):
stack=[]
st=string.split(' ')
for i in st:
stack.append(i)
top=len(stack)
while(len(stack)>0):
print(stack.pop(),end=' ')
print()
def reverseWords(string):
st=[]
for i in range(len(string)):
if string[i]!=" ":
st.append(string[i])
else:
while(len(st)>0):
print(st.pop(),end='')
print(end=" ")
st=input("Enter the string: \t")
st=st+" "
ch='y'
while(ch=='y'):
print("\n1. Reverse line without reversing individual words. \n2. Reverse individual words without
reversing the line.\n")
choice=int(input("Enter your choice: \t"))
if choice==1:
reverseWords(st)
elif choice==2:
reverseLine(st)
ch=input("\nDo you wish to continue (y/n): \t")
OUTPUT:
def brackets(s):
stk=[]
top=0
flag=0
for i in s:
top=len(stk)-1
if i in ['(','[','{']:
stk.append(i)
top=len(stk)-1
if i in[')',']','}'] and stk!=[]:
if ((i==')' and stk[top]=='(') or (i==']' and stk[top]=='[') or (i=='}' and stk[top]=='{')):
stk.pop()
top=len(stk)-1
else:
flag+=1
break
OUTPUT(1):
OUTPUT(2):