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

Stack

Uploaded by

hod.it
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Stack

Uploaded by

hod.it
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

def isEmpty(stk):

if stk==[]:

return True

else:

return False

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 Peek(stk):

if isEmpty(stk):

return "Underflow"

else:

top = len(stk) - 1

return stk[top]

1
def Display(stk):

if isEmpty(stk):

print( "Stack Empty")

else:

top = len(stk) - 1

print (stk[top],"<-top")

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

print (stk[a])

# main function

Stack = []

top = None

while True:

print ("Stack Operations")

print ("1.Push")

print ("2.Pop")

print ("3.Peek")

print ("4.Display Stack")

print ("5.Exit")

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

if ch==1:

item = int(input("Enter Item:"))

Push(Stack,item)

elif ch==2:

item=Pop(Stack)

if item == "Underflow":

print ("Underflow! Stack is Empty")

2
else:

print ("Popped Item is:",item)

elif ch==3:

item=Peek(Stack)

if item == "Underflow":

print ("Underflow! Stack is Empty")

else:

print ("TopMost Item is:",item)

elif ch==4:

Display(Stack)

elif ch==5:

break

else:

print ("Invalid Choice!")

To push cityname and pincode

city=[]

def PUSHCITY(cityname,pincode):

city.insert(0,[cityname,pincode])

OR

city=[]

def POPCITY():

if len(city)==0:

print(“Underflow”)

return None

return city.pop()

3
Another Method

def PushBook(Book):

bno = input("enter book no : ")

btitle = input(“enter book title:”)

rec = bno + “ ” + btitle

Book.append(rec)

print(Book)

OR

def PopBook(Book) :

# If stack is empty

if len(Book)==0:

print("Underflow")

else:

print(“Deleted entry :”, Book.pop())

You might also like