STACK
Program:
#Implementation of stack in python using a list
S=[ ]
print("[Link] the elements")
print("[Link] an element")
print("[Link] the elements")
print("[Link]")
while True:
a=int(input("Enter your choice: "))
#inserting an element
if a==1:
no=int(input("enter book number"))
name=input("enter book name"))
l=[no,name]
[Link](l)
print("the stack is :", s)
# Deleting the element
elif a==2:
if s==[]:
print("stack is empty")
else:
print("deleted element is",[Link]())
# Displaying the element
elif a==3:
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
elif a==4:
print("exiting this program")
print("thankyou")
break
Output :
[Link] the elements
[Link] an element
[Link] the elements
[Link]
Enter your choice: 1
enter book number221
enter book nameThe God That Failed
the stack is : [[221, 'The God That Failed']]
Enter your choice: 1
enter book number222
enter book nameThe Lost Town
the stack is : [[221, 'The God That Failed'], [222, 'The Lost Town']]
Enter your choice: 1
enter book number223
enter book namePride and Prejudice
the stack is : [[221, 'The God That Failed'], [222, 'The Lost Town'], [223, 'Pride and Prejudice']]
Enter your choice: 1
enter book number224
enter book nameThe Merchant of Venice
the stack is : [[221, 'The God That Failed'], [222, 'The Lost Town'], [223, 'Pride and Prejudice'], [224,
'The Merchant of Venice']]
Enter your choice: 1
enter book number225
enter book nameMacbeth
the stack is : [[221, 'The God That Failed'], [222, 'The Lost Town'], [223, 'Pride and Prejudice'], [224,
'The Merchant of Venice'], [225, 'Macbeth']]
Enter your choice: 3
[225, 'Macbeth']
[224, 'The Merchant of Venice']
[223, 'Pride and Prejudice']
[222, 'The Lost Town']
[221, 'The God That Failed']
Enter your choice: 2
deleted element is [225, 'Macbeth']
Enter your choice: 3
[224, 'The Merchant of Venice']
[223, 'Pride and Prejudice']
[222, 'The Lost Town']
[221, 'The God That Failed']
Enter your choice: 4
Exiting this program
thank you