Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
26 views1 page

Stack

The document contains a Python implementation of a stack data structure with functions to push, pop, peek, and display elements. It includes a loop for user interaction to perform these operations based on input options. The code also handles underflow conditions when attempting to pop or peek from an empty stack.

Uploaded by

Manvi Gupta
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)
26 views1 page

Stack

The document contains a Python implementation of a stack data structure with functions to push, pop, peek, and display elements. It includes a loop for user interaction to perform these operations based on input options. The code also handles underflow conditions when attempting to pop or peek from an empty stack.

Uploaded by

Manvi Gupta
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/ 1

l=[]

def push(li):
e=int(input("Enter the element to push"))
li.append(e)

def pop(li):
if ((len(li)!=0)):
e=li.pop()
print("Popped item is",e)
else:
print("Underflow")

def peek(li):
if ((len(li)!=0)):
print("Topmost item is",li[-1])
else:
print("Underflow")

def display(li):
if ((len(li)!=0)):
top=len(li)-1
print(li[top],"<-top")
for item in range((top-1),-1,-1):
print(li[item])
else:
print("Stack is Empty")

while True:
op=int(input("Enter the option from 1 to 5: "))
if op==1:
push(l)
elif op==2:
pop(l)
elif op==3:
peek(l)
elif op==4:
display(l)
elif op==5:
break
else:
print("Valid option")

You might also like