14 Stack
14 Stack
Insertio
n from
TOP
9 TOP
Insertio
n from
TOP
TOP
8
9
Insertio
n from
TOP TOP
3
8
9
Insertio
TOP
n from 5
TOP
3
8
9
TOP
Insertio 3
n from 5
TOP
3
8
9
TOP
7
Insertio 3
n from 5
TOP STACK
3
8 FULL
9
TOP
7
Deletion 3
from 5
TOP
3
8
9
TOP
Deletion 3
from 5
TOP
3
8
9
Deletion
TOP
from 5
TOP
3
8
9
Deletion
from
TOP TOP
3
8
9
Deletion
from
TOP
TOP
8
9
Deletion
from
TOP
TOP
9
Deletion
from
TOP
STACK EMPTY
IMPLEMENTI
NG
STACK
USING
LIST
def Push(stk,item):
stk.append(item)
stack=[ ]
while True:
print("Stack Operations ")
print("1. Push ")
print("5. Exit ")
ch=int(input("Enter your choice "))
if ch==1:
item=int(input("Enter no. to insert "))
Push(stack, item)
elif ch==5:
break
else:
print("Invalid choice ")
def Display(stk):
if isempty(stk): stack=[ ]
print("Stack Khali hai ") while True:
else: print("Stack Operations ")
top=len(stk)-1 print("1. Push ")
print(stk[top],"<-top") print("4. Display Stack ")
for a in range(top-1,-1,-1): print("5. Exit ")
print(stk[a]) ch=int(input(“Choice?? "))
if ch==1:
def isempty(stk): item=int(input("Enter no"))
if stk==[]: Push(stack, item)
return True elif ch==4:
else: Display(stack)
return False elif ch==5:
break
def Push(stk,item): else:
stk.append(item) print("Invalid choice “)
def Display(stk):
if isempty(stk):
print("Stack Khali hai ")
else:
top=len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
def isempty(stk):
if stk==[ ]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
def Pop(stk):
if isempty(stk):
return "Underflow "
else:
item=stk.pop()
return item
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]
stack=[ ]
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 "))
if ch==1:
item=int(input("Enter no. to insert "))
Push(stack, item)
elif ch==2:
item=Pop(stack)
if item=="Underflow ":
print("Stack is empty, nothing to delete ")
else:
print("Delete hua ",item)
elif ch==3:
item=peek(stack)
if item=="Underflow ":
print("Nothing to delete ")
else:
print("Topmost item is ",item)
elif ch==4:
Display(stack)
elif ch==5:
break
else:
print("Invalid choice ")
IMPLEMENTI
NG
STACK
USING
DICTIONARY
def Display(stk):
if isempty(stk):
print("Stack Khali
hai ")
else:
for a,b in
stk.items():
print(a," ",b)
def isempty(stk):
if
len(stk["Roll"])==0:
return True
else:
return False
def Push(stk,item1, item2):
stk["Roll"].append(item1)
stk["Name"].append(item2)
def Pop(stk):
if isempty(stk):
return "Underflow "
else:
b=0
for a in stk["Roll"]:
b=b+1
c=stk["Roll"][b-1]
d=stk["Name"][b-1]
stk["Roll"].pop(b-1)
stk["Name"].pop(b-1)
print(stk)
return c,d
def peek(stk):
if isempty(stk):
return "Underflow "
else:
b=0
for a in stk["Roll"]:
b=b+1
return stk["Roll"][b-
1],stk["Name"][b-1]
stack={"Roll":[],"Name":[]} elif ch==3:
while True: item=peek(stack)
print("Stack Operations ") if item=="Underflow ":
print("1. Push ") print("Nothing to
delete ")
print("2. Pop ") else:
print("3. Peek ") print("Topmost
item is ",item)
print("4. Display Stack ") elif ch==4:
print("5. Exit ") Display(stack)
ch=int(input("Enter your choice ")) elif ch==5:
if ch==1: break
item1=int(input("Enter Roll to insert ")) else:
item2=input("Enter Name to insert ") print(“Invalid
Choice “)
Push(stack, item1, item2)
elif ch==2:
item=Pop(stack)
if item=="Underflow ":
print("Stack is empty, nothing to delete ")
else:
print("Delete hua ",item)
IMPLEMENTI
NG
STACK
USING
NESTED LIST
def Display(stk):
if isempty(stk):
print("Stack Khali hai
")
else:
for a in range(len(stk)-
1,-1,-1):
print(stk[a])
def isempty(stk):
if len(stk)==0:
return True
else:
return False
def Push(stk,item):
stk.append(item)
def Pop(stk):
if isempty(stk):
return
"Underflow "
else:
x=stk.pop()
return x
def peek(stk):
if isempty(stk):
return
"Underflow "
else:
return
stk[len(stk)-1]
stack=[] elif ch==3:
while True: item=peek(stack)
print("Stack Operations ") if item=="Underflow ":
print("1. Push ") print("Nothing
to delete ")
print("2. Pop ") else:
print("3. Peek ") print("Topmost
item is ",item)
print("4. Display Stack ") elif ch==4:
print("5. Exit ") Display(stack)