0% found this document useful (0 votes)
12 views

Stack Program

Uploaded by

kumarrohith3168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Stack Program

Uploaded by

kumarrohith3168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

24] Write a python program to perform the operations on stack.

def push():
ele=int(input("enter the element to be inserted"))
stack.append(ele)

def pop():
if stack==[]:
print("stack is empty/underflow")
else:
print("the delete element is",stack.pop())

def peek():
top=len(stack)-1
print("the top most element of the stack is",stack[top])

def display():
top=len(stack)-1
print("the stack elements are")
for i in range(top,-1,-1):
print(stack[i])

#main program
stack=[]
opt='y'
while opt=='y' or opt=='Y':
print("stack operation")
print("1.push")
print("2.pop")
print("3.peek")
print("4. display")
opt=int(input("enter your choice"))
if opt==1:
push()
elif opt==2:
pop()
elif opt==3:
peek()
elif opt==4:
display()
else:
print("invalid option")
opt=input("do you want to perform another stack
operation?(y/n)")

You might also like