Professional Documents
Culture Documents
Grade 12 CS Board Practicals STACK
Grade 12 CS Board Practicals STACK
STACKS
SET 1
def Push(data):
global top
#insert the element on the top of the stack
Stack.append(data)
top=top+1 #points to the last element inserted into the
stack
def Pop():
global top
if Stack==[]: #there are no elements in the stack
print("UNDERFLOW")
else:
item=Stack.pop() # delete the element on the top of
the stack or last element inserted
top=top-1 #holds the index of the last element
print("\nPOPPED ITEM IS :", item)
def Peek():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else:
print("THE ELEMENT ON THE TOP OF STACK
IS:",Stack[top]) #The element pointed to by top
def Display():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else:
print("\n\t\tTHE STACK CONTENT\n")
print("DOCID", "NAME", "DEPARTMENT")
print("__________________________________")
for i in range(top,-1,-1): #printing elements from
the top to the first element inserted
print(Stack[i])
Using user defined functions, write a Menu Based Program to implement a stack where each
node holds details of players. Take care of overflow/underflow situations. The Menu should
be:
1. Push
2. Pop
3. Display entire stack content.
4. Exit
Each node of the Stack will have the following structure:
{'name':_____, 'points': ______}
def Push(data):
global top
if len(Stack)==size:
print("OVERFLOW")
else:
#insert the element on the top of the stack
Stack.append(data)
top=top+1 #The last element inserted into the stack
def Pop():
global top
if Stack==[]: #there are no elements in the stack
print("UNDERFLOW")
else:
item=Stack.pop() # delete the element on the top of
the stack
top=top-1 #holds the index of the last element
print("\nPOPPED ITEM IS :", item)
def Display():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else:
print("\n\t\tTHE STACK CONTENT\n")
print("NAME", "POINTS")
print("__________________________________")
for i in range(top,-1,-1): #printing elements from
the top to the first element inserted
print(Stack[i])
#initially stack is empty
Stack=[] #EMPTY LIST
top=-1
size=5
while True:
print("\n\t\tSTACK OPERATIONS")
print("_________________________________________")
print("\t\t1:PUSH")
print("\t\t2:POP")
print("\t\t3:DISPLAY ENTIRE STACK CONTENT")
print("\t\t4:DISPLAY TOP NODE")
print("\t\t5:EXIT")
ch=int(input("\nENTER YOUR CHOICE (1-5):"))
if ch==1:
data={}
data['name'] = input("name:")
data['points'] = int(input("Enter points:"))
Push(data)
elif ch==2:
Pop()
elif ch==3:
Display()
elif ch==4:
break
else:
print("INVALID CHOICE!!")
PART I : LAB TEST- 1: (Python Programming: 8 marks)
Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks
Using user defined functions, write a Menu Based Program to implement a stack that
holds details of books. Take care of overflow/underflow situations. The Menu should be
1. Push
2. Pop
3. Display entire stack content
4. Display Top Node
5. Exit
Each node of the Stack will have the following structure
[BOOK_ID, TITLE, AUTHOR, PRICE]
def Push(data):
global top
#insert the element on the top of the stack
Stack.append(data)
top=top+1 #points to the last element inserted into the stack
def Pop():
global top
if Stack==[]: #there are no elements in the stack
print("UNDERFLOW")
else:
item=Stack.pop() # delete the element on the top
top=top-1 #holds the index of the last element
print("\nPOPPED ITEM IS :", item)
def Peek():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else:
print("THE ELEMENT ON THE TOP OF STACK IS:",Stack[top])
#prints the element pointed to by top
def Display():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else:
def Pop():
global top
if Stack==[]: #there are no elements in the stack
print("UNDERFLOW")
else:
item=Stack.pop() # deletes the element on the top
top=top-1 #holds the index of the last element
print("\nPOPPED ITEM IS :", item)
def Peek():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else:
print("THE ELEMENT ON THE TOP OF STACK IS:",Stack[top])
#prints the element pointed to by top
def Display():
global top
if Stack==[]:
print("UNDERFLOW!STACK IS EMPTY!!")
else: