1def isEmpty(stk): # checks whether the stack is empty or not
2 if stk==[]:
3 return True
4 else:
5 return False
6
7def Push(stk,item): # Allow additions to the stack
8 stk.append(item)
9 top=len(stk)-1
10
11def Pop(stk):
12 if isEmpty(stk): # verifies whether the stack is empty or not
13 print("Underflow")
14 else: # Allow deletions from the stack
15 item=stk.pop()
16 if len(stk)==0:
17 top=None
18 else:
19 top=len(stk)
20 print("Popped item is "+str(item))
21
22def Display(stk):
23 if isEmpty(stk):
24 print("Stack is empty")
25 else:
26 top=len(stk)-1
27 print("Elements in the stack are: ")
28 for i in range(top,-1,-1):
29 print (str(stk[i]))
30
31# executable code
32if __name__ == "__main__":
33 stk=[]
34 top=None
35 Push(stk,1)
36 Push(stk,2)
37 Push(stk,3)
38 Push(stk,4)
39 Pop(stk)
40 Display(stk)
1#Adding elements to queue at the rear end
2def enqueue(data):
3 queue.insert(0,data)
4
5#Removing the front element from the queue
6def dequeue():
7 if len(queue)>0:
8 return queue.pop()
9 return ("Queue Empty!")
10
11#To display the elements of the queue
12def display():
13 print("Elements on queue are:");
14 for i in range(len(queue)):
15 print(queue[i])
16
17# executable code
18if __name__=="__main__":
19 queue=[]
20 enqueue(5)
21 enqueue(6)
22 enqueue(9)
23 enqueue(5)
24 enqueue(3)
25 print("Popped Element is: "+str(dequeue()))
26 display()