0% found this document useful (0 votes)
17 views

Lab Programs 3

The document describes a menu driven program to perform various operations on linear lists and queues representing hostel records. The program allows the user to add, delete and display records from the list/queue. It implements functions for linear search, traversal, modification and deletion of list elements. Similarly for queues, functions are defined for enqueue, dequeue and display of records. The program uses a while loop and menu to repeatedly call these functions until the user chooses to exit.

Uploaded by

Minmini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab Programs 3

The document describes a menu driven program to perform various operations on linear lists and queues representing hostel records. The program allows the user to add, delete and display records from the list/queue. It implements functions for linear search, traversal, modification and deletion of list elements. Similarly for queues, functions are defined for enqueue, dequeue and display of records. The program uses a while loop and menu to repeatedly call these functions until the user chooses to exit.

Uploaded by

Minmini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DATA STRUCTURES

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.

SNIPPET / SOURCE CODE:

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:

Enter the list items with space between them :


11 22 33 55 10
1. Linear search
2. Traverse
3. Modify
4. Add element to the list
5. Delete an element from the list

Enter your choice: 1


Enter the element to be searched: 55
55 found at index value 3

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

Enter your choice: 2

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

Enter your choice: 3

Enter the element to be modified: 9


Element not found

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

Enter your choice: 3

Enter the element to be modified: 33


Element found. Enter new value:
99
List modified

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

Enter your choice: 4

Enter the element to be added to the list:


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

Enter your choice: 2

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

Enter your choice: 5

Enter the element to be deleted: 99


Element deleted successfully

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

Enter your choice: 2

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

SNIPPET / SOURCE CODE:

# To check if stack is empty


def isEmpty(host):
if host==[]:
return True
else:
return False

# Add record to the stack


def push(host):
hn=int(input("Enter the hostel number: "))
ts=int(input("Enter the number of students : "))
tr=int(input("Enter the total number of rooms: "))
temp=[hn,ts,tr]
host.append(temp)
top=len(host)-1

#Delete record from the stack


def pop(host):
if isEmpty(host):
print("Stack is empty")
else:
t=host.pop()
if len(host)==0:
top=None
else:
top=len(host)-1
print("The popped record is :\nHostel No \tNo. of Students \tTotal Rooms\n")
for i in range(0,len(t)):
print(t[i],end="\t\t")
print()

#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

Enter your choice: 1


Enter the hostel number: 11
Enter the number of students : 50
Enter the total number of rooms: 25
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 1


Enter the hostel number: 12
Enter the number of students : 30
Enter the total number of rooms: 15
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 1


Enter the hostel number: 13
Enter the number of students : 40
Enter the total number of rooms: 20
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 3


Hostel No No. of Students Total Rooms
13 40 20
12 30 15
11 50 25
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 2


The popped record is :
Hostel No No. of Students Total Rooms

13 40 20
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 3


Hostel No No. of Students Total Rooms
12 30 15
11 50 25
Do you wish to continue:(y/n) n
>>>
DATA STRUCTURES

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

SNIPPET / SOURCE CODE:


# To check if queue is empty
def isEmpty(host):
if host==[]:
return True
else:
return False

# Add record to the queue


def enqueue(host):
hn=int(input("Enter the hostel number: "))
ts=int(input("Enter the number of students : "))
tr=int(input("Enter the total number of rooms: "))
temp=[hn,ts,tr]
host.append(temp)
if len(host)==1:
front=rear=0
else:
rear=len(host)-1

#Delete record from the queue


def dequeue(host):
if isEmpty(host):
print("Queue is empty")
else:
t=host.pop(0)
if len(host)==0:
front=rear=None
print("The popped record is :\nHostel No \tNo. of Students \tTotal Rooms\n")
for i in range(0,len(t)):
print(t[i],end="\t\t")
print()

#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

Enter your choice: 1


Enter the hostel number: 11
Enter the number of students : 50
Enter the total number of rooms: 25
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 1


Enter the hostel number: 12
Enter the number of students : 30
Enter the total number of rooms: 15
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 1


Enter the hostel number: 13
Enter the number of students : 40
Enter the total number of rooms: 20
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 3


[Hostel No,No. of Students,Total Rooms]
[11, 50, 25] <--front
[12, 30, 15]
[13, 40, 20] <--rear
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 2


The popped record is :
Hostel No No. of Students Total Rooms

11 50 25
Do you wish to continue:(y/n) y
1. Add record
2. Delete Record
3.Display

Enter your choice: 3


[Hostel No,No. of Students,Total Rooms]
[12, 30, 15] <--front
[13, 40, 20] <--rear
Do you wish to continue:(y/n) n
DATA STRUCTURES

PROGRAM 7: Write a menu driven program to


(a) Reverse a line without reversing individual words
(b) Reverse individual words without reversing the line

SNIPPET / SOURCE CODE:

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:

Enter the string: This is the line

1. Reverse line without reversing individual words.


2. Reverse individual words without reversing the line.

Enter your choice: 1


sihT si eht enil
Do you wish to continue (y/n): y

1. Reverse line without reversing individual words.


2. Reverse individual words without reversing the line.

Enter your choice: 2


line the is This

Do you wish to continue (y/n): n


DATA STRUCTURES

PROGRAM 8: Check for Balanced Brackets in an expression (well-formedness) using Stack

SNIPPET / SOURCE CODE:

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

elif stk==[] and i in[')',']','}']:


flag+=1
break
if flag==0 and len(stk)==0:
print("Brackets are well balanced")
else:
print("Brackets are improper!")

x=input("Enter expression: \t")


brackets(x)

OUTPUT(1):

Enter expression: 5 + 2{[3 + (2x – 1) + x] – 2}

Brackets are well balanced

OUTPUT(2):

Enter expression: 5{3 – 2[1 – 4(3 – 22)]

Brackets are improper!

You might also like