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

Stack Implementation using List

This document outlines a stack implementation using a list in Python, including functions for checking if the stack is empty, peeking at the top item, pushing and popping items, and displaying the stack. It also includes a main loop for user interaction to perform various stack operations. The stack starts empty and allows users to execute operations until they choose to exit.

Uploaded by

thanoshu55
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)
7 views2 pages

Stack Implementation using List

This document outlines a stack implementation using a list in Python, including functions for checking if the stack is empty, peeking at the top item, pushing and popping items, and displaying the stack. It also includes a main loop for user interaction to perform various stack operations. The stack starts empty and allows users to execute operations until they choose to exit.

Uploaded by

thanoshu55
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/ 2

########### STACK IMPLEMENTATION ###########

#"""
#Stack : implemented as a list
#top : integer having position of topmost element in Stack
#"""

def isEmpty (stk) :


if stk == []:
return True
else :
return False

def Peek (stk) :


if isEmpty (stk) :
return "Underflow"
else :
top = len (stk) - 1
return stk [top]

def Push (stk, item) :


stk.append (item)
top = len (stk) - 1

def Pop (stk) :


if isEmpty (stk) :
return "Underflow"
else :
item = stk.pop ()
if len (stk) == 0 :
top = None
else :
top = len (stk) - 1
return item

def Display (stk) :


if isEmpty (stk) :
print ("Stack Empty.")
else :
top = len (stk) - 1
print (stk [top], "<- top")

for i in range (top-1, -1, -1) :


print (stk[i])
#__main__

Stack = [] #Stack is intially empty


top = None

while True :
print ("STACK OPERATIONS")
print ("1. Peek")
print ("2. Push")
print ("3. Pop")
print ("4. Display Stack")
print ("5. Exit")

ch = int (input ("Enter your choice (1 - 5) : "))

if ch == 1 :
item = Peek (Stack)
if item == "Underflow" :
print ("Underflow! Stack is empty!")
else :
print ("Topmost item is ", item)
elif ch == 2 :
item = int (input ("Enter item : "))
Push (Stack, item)
elif ch == 3 :
item = Pop (Stack)
if item == "Underflow" :
print ("Underflow! Stack is empty!")
else :
print ("Popped item is ", item)
elif ch == 4 :
Display (Stack)
elif ch == 5 :
break
else :
print ("Invalid choice!")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy