Stacks using Lists
Data Structure-Data structure refers to a particular way of storing and organizing data in a
computer so that it can be used most efficiently to give optimal performance. Some data sturcture
are stacks and queues.
Q. Program to implement stack operations.
stk=[ ]
c="y"
while(c=="y"):
print("[Link]")
print("[Link]")
print("[Link]")
choice=int(input("enter your choice"))
if(choice==1):
a=input("enter any number :")
[Link](a)
elif(choice==2):
if(stk==[]):
print("stack empty")
else:
print("deleted element:", [Link]())
elif(choice==3):
l=len(stk)
for i in range(l-1,-1,-1):
print(stk[i])
else:
print("wrong input")
c=input("do u want to continue")
Q. 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
Ans.
Q. 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
Ans.
Q. Write PushOn() and PopUp() methods/functions in Python to
add a new Book and delete a Book from a List of Book titles,
considering them to act as push and pop operations of the
Stack data structure.
Ans.
Book=[ ]
def PushOn():
BT=input("enter book title :")
[Link](BT)
def PopUp():
if (Book==[ ]):
print ("Stack empty")
else:
print("Deleted element:", [Link]())
Q. Write ADDBOOK( ) and REMOVEBOOK( ) methods in python
to add book’s Information such as Book Id (BID) and Book
Price(BP) considering them to act as Push and Pop
operations of stack.
Ans.
Book=[ ]
def ADDBOOK( ):
BID=input("enter book ID :")
BP=input("enter book Price :")
C=(BID, BP)
[Link](C)
def REMOVEBOOK( ):
if (Book==[ ]):
print ("Stack empty")
else:
print("Deleted element:",[Link]())