0% found this document useful (0 votes)
11 views2 pages

Stack

The document outlines a Python program that implements a stack using a list, allowing users to insert, delete, and display elements. Users can input book numbers and names to manage a stack of books, with options to view the current stack or exit the program. The program includes error handling for empty stacks and provides a simple user interface for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Stack

The document outlines a Python program that implements a stack using a list, allowing users to insert, delete, and display elements. Users can input book numbers and names to manage a stack of books, with options to view the current stack or exit the program. The program includes error handling for empty stacks and provides a simple user interface for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like