Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (4 votes)
8K views

Program-1: Given A Stack Named Book - Details That Contains Book - No, Book - Name and

The document provides code for 3 programs - the first implements stack operations like push and pop on a book details stack, the second pushes student names from a dictionary into a stack if their marks are over 75 and pops them off, and the third pushes even numbers from a list into a stack and pops them off. Separate functions are defined for push, pop, and display operations, and main programs call these functions to demonstrate the stack implementation.

Uploaded by

Soumya tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
8K views

Program-1: Given A Stack Named Book - Details That Contains Book - No, Book - Name and

The document provides code for 3 programs - the first implements stack operations like push and pop on a book details stack, the second pushes student names from a dictionary into a stack if their marks are over 75 and pops them off, and the third pushes even numbers from a list into a stack and pops them off. Separate functions are defined for push, pop, and display operations, and main programs call these functions to demonstrate the stack implementation.

Uploaded by

Soumya tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program on Stack(To write in Program File)

Program-1: Given a stack named Book_Details that contains Book_No, Book_Name and
Price. Write code to implement the following functions for the stack.
A. Push(Book_Details):- a function to add a book in the stack, the details of Book_No,
Book_Name and Price should be entered in the function
B. POP(Book_Details):- a function to delete a book from the stack and return its details.

C. DISP():- to display all details from stack Book_Details.


def PUSH(Book_Details,Book):
Book_Details.append(Book)

def POP(Book_Details):
if len(Book_Details)==0: # STK==[]:
print("Stack Empty!!!")
return None
else:
element=Book_Details.pop()
return element

def DispStack(Book_Details):
if len(Book_Details)==0:
print("Stack Empty!!!")
return None
else:
for i in range(len(Book_Details)-1,-1,-1):
print(Book_Details[i])

# main program section

Book_Details=[] # stack=[]
ch='y' # while loop
while ch=='y'or ch=='Y':
print("------------ STACK OPERATION MENU -------------")
print("1. PUSH")
print("2. POP")
print("3. Display ")
print("4. Exit")
N=int(input("Enter your choice(1-4):"))
if N==1:
BookNo=int(input("Input the Book No to Push:"))
BookName=input("Input the Book Name:")
Price=float(input("Input the Price:"))
Book=[BookNo,BookName,Price]
PUSH(Book_Details,Book)
elif N==2:
Val=POP(Book_Details)
if Val!=None:
print("Popped data=:",Val)
elif N==3:
DispStack(Book_Details)
elif N==4:
pass
ch=input("\nDo you want to continue Y/N?")

Program2: Julie has created a dictionary containing names and marks as key value pairs of 6
students. Write a program, with separate user defined functions to perform the following
operations:
● Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
● Pop and display the content of the stack. For example: If the sample content of the
dictionary is as follows:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be: TOM ANU BOB OM

Stk=[] # global variable


D={}
def PUSH():
for n,m in D.items():
if m>75:
Stk.append(n)
def POP():
while True:
if Stk[]!=[]:
print(Stk.pop(),end=" ")
else:
break

# main program
# This part of program enters the details in to the dictionary
while True:
name=input("Enter the name:")
marks=int(input("Enter the marks:"))
D[name]=marks
ch=input("Want to add moreY/N?")
if ch=='n' or ch=='N':
break
PUSH() # FUNCTION CALL OF PUSH
POP() # FUNCTION CALL OF POP
Program-3 Alam has a list containing 10 integers. You need to help him create a program
with separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack. For Example: If the sample Content of the list is
as follows: N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 38 22 98 56 34 12
Stk=[] # global variable
def PUSH(L):
for i in L:
if i%2==0:
Stk.append(i)
def POP():
while True:
if Stk!=[]:
print(Stk.pop(),end=" ")
else:
break

# main program
# This part of program enters the details in to the list
L=list(eval(input("Enter the elements of the list: ")))
print(L)
PUSH(L)
POP()

You might also like