STACKS
SR INPUT OUTPUT
NO.
1.
def isempty(stk): STACK OPERATIONS
if stk==[]: 1.PUSH
return True 2.POP
else: 3.PEEK
return False 4.DISPLAY
5.EXIT
def Push (stk,item): Enter your choice(1-5) :
stk.append(item)
global top
top=len(stk)-1
def Pop(stk):
if isempty(stk):
return"underflow"
else:
item = stk.pop()
if len(stk)==0:
top=-1
else:
top=len(stk)-1
return item
def Peek(stk):
if isempty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
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__
stack=[]
top = None
while True:
print("STACK OPERATIONS")
print("1.PUSH")
print("2.POP")
print("3.PEEK")
print("4.DISPLAY ")
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("Stack is empty")
else:
print("Popped item is", item)
elif ch == 3:
item = Peek(stack)
if item =="Underflow":
print("stack is empty")
else:
print("topmost item is ", item)
elif ch == 4:
Display(stack)
elif ch == 5:
break
else:
print("invalid choice")
2. newlist=[]
def push_element(city): [['mumbai', 98], ['pune',
for x in city: 90]]
if x[0] != 'vadodara' and x[1]>=90: ['pune', 90]
newlist.append(x) ['mumbai', 98]
print(newlist) stack empty
def pop_element():
while len(newlist):
print(newlist.pop())
else:
print("stack empty")
city=[['mumbai',98],['pune',90],['vadodara',80],['delhi',79]]
push_element(city)
pop_element()
3. newlist=[] ['mumbai', 'pune', 'delhi']
def push_element(city): delhi
for x in city: pune
if x != 'vadodara': mumbai
newlist.append(x) stack empty
print(newlist)
def pop_element():
while len(newlist):
print(newlist.pop())
else:
print("stack empty")
city=['mumbai','pune','vadodara','delhi']
push_element(city)
pop_element()