0% found this document useful (0 votes)
3 views3 pages

Stack Program

The document provides a Python implementation of a stack using a list, including functions for pushing, popping, peeking, and displaying stack elements. It features a user interface that allows users to interact with the stack through a menu-driven approach. The code includes error handling for empty stacks and invalid inputs.

Uploaded by

abishek241008
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)
3 views3 pages

Stack Program

The document provides a Python implementation of a stack using a list, including functions for pushing, popping, peeking, and displaying stack elements. It features a user interface that allows users to interact with the stack through a menu-driven approach. The code includes error handling for empty stacks and invalid inputs.

Uploaded by

abishek241008
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
You are on page 1/ 3

NATIONAL PUBLIC SCHOOL, GOPALAPURAM

GRADE XII A&B:


CHAPTER : DATA STRUCTURES
#Implementation of Stack using List
def Push(ele,s):
'''if ele%2==0:
s.append(ele)
top=len(s)-1#-1'''
for k,v in d.items():
if v>75:
s.append(k)
return
def Pop(s):
if s==[]:
return "Stack is empty"
else:
v=s.pop()
if s==[]:
top=None
else:
top=len(s)-1
return v

def display(s):
if s==[]:
print('Stack empty')
else:
#print(s[::-1])
'''a=s[::-1]
for i in a:
if i==top:
print(i,"top")
print(i)'''
top=len(s)-1
print(s[top],"top")
for i in range(top-1,-1,-1):
print(s[i])
def Peek(s):
if s==[]:
print('Stack empty')
else:
top=len(s)-1
print(s[top])

s=[]#stack
top=None
while True:
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display")
print("5.Exit")
c=int(input("Enter your choice:"))
if c==1:
e=int(input("Enter the element to be pushed:"))
Push(e,s)
print('Data added')
elif c==2:
d=Pop(s)
print(d)
elif c==3:
Peek(s)
elif c==4:
display(s)
elif c==5:
break
else:
print(“Invalid Input”)

You might also like