Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views4 pages

Stack

The document outlines several programming tasks involving stack and queue data structures in Python. It includes functions to push and pop student records, manage stationary items based on price, and handle book and hostel records using menu-driven programs. Each task is accompanied by example code demonstrating the required functionality.

Uploaded by

yuvaram9790
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Stack

The document outlines several programming tasks involving stack and queue data structures in Python. It includes functions to push and pop student records, manage stationary items based on price, and handle book and hostel records using menu-driven programs. Each task is accompanied by example code demonstrating the required functionality.

Uploaded by

yuvaram9790
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Stack

1. A list contains following record of a student:


[StudentName, Class, Section,
MobileNumber]
Write the following user defined functions to perform given operations on the stack
named ‘xiia’:
(i) pushElement() - To Push an object containing name and mobile number of students
whobelong to class xii and section ‘a’ to the stack
(ii) popElement() - To Pop the objects from the stack and display them. Also,
display “Stack
Empty” when there are no elements in the
stack. For example:
If the lists of students details are:
[“Rajveer”, “99999999999”,”XI”, “B”]
[“Swatantra”, “8888888888”,”XII”, “A”]
[“Sajal”,”77777777777”,”VIII”,”A”]
[“Yash”, “1010101010”,”XII”,”A”]
The stack “xiia” should
contain [“Swatantra”,
“8888888888”]
[“Yash”, “1010101010”]
The output should be:
[“Yash”, “1010101010”]
[“Swatantra”, “8888888888”]

Stack Empty

ANSWER:

students = [
["Rajveer", "99999999999","XI", "B"],
["Swatantra", "8888888888","XII", "A"],
["Sajal","77777777777","VIII","A"],
["Yash", "1010101010","XII","A"] ]

XII_A = []
def pushElement():
for st in students:
if st[2] == 'XII' and st[3] == 'A':
rec = [st[0], st[1]]
XII_A.append(st)

def popElement():
for i in range(len(XII_A)+1):
if XII_A == []:
print("Empty Stack")
else:
st = XII_A.pop()
print(st)

#__ main block ___


print("PUSHING Students\n")
pushElement()

#to invoke pop for six times


print("\nPOPPING")
popElement()

2. Write a function in Python, Push(SItem) where, SItem is a dictionary containing the


details ofstationary items– {Sname:price}.

The function should push the names of those items in the stack who have price greater
than 25.
Also display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Ditem = {“Rubber”:5, "Pencil":5, "Pen":30, "Notebook": 60, "Eraser":5, “Watch”:
250} The stack should contain
Pen
Notebook
Watch
The output should be:
The count of elements in the stack is 3
Answer:

Stackitem=[]
def Push(Sitem):
C=0
for k in Sitem:
if (Sitem[k]>=25):
Stackitem.append(k)
C=C+1
print(“The count of elements in the stack is:”, C)
3. Write a menu driven program using function Qadd( ), Qdel( ) and disp( ) to add, delete and
display the record of book using queue as data structure in python. Record of book store the
following details : Book name, Book Number and Book Price.
Answer:
book=[ ]
ch='y'
def Qadd(book):
bn=input("Enter book name")
bnum=int(input("Enter book number"))
bp=int(input("Enter book price"))
temp=[bn,bnum,bp]
book.append(temp)
def Qdel(book):
if(book==[ ]):
print("No Record")
else:
print("Deleted Record is :",book.pop(0))
def disp(book):
l=len(book)
print("Book Name\tBook Number\tBook Price")
for i in range(0,l):
print(book[i][0],"\t\t",book[i][1],"\t\t",book[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
print("4. Exit")
op=int(input("Enter the Choice"))
if(op==1):
Qadd(book)
elif(op==2):
Qdel(book)
elif(op==3):
disp(book)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N)")

4. Write a function Add(book) and Del(book) to add a new book name and remove a book name
from a list book, considering them to act as insert and delete operations of the queue Data
Structure in Python.
book=[ ]
def Add(book):
bn=input("Enter name of book")
book.append(bn)

def Del(book):
if(book==[ ]):
print("Queue is empty")
else:
print("Deleted book name :",book.pop(0))

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.

host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number"))
ts=int(input("Enter Total students"))
tr=int(input("Enter total rooms"))
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
print("Deleted Record is :",host.pop())
def display(host):
l=len(host)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
print("4. Exit")
op=int(input("Enter the Choice"))
if(op==1):
push(host)
elif(op==2):
pop(host)
elif(op==3):
display(host)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N)")

You might also like