Q. Write a function in Python POP(Arr), where Arr is a stack implemented by a list of numbers. The function returns the value deleted from the stack.
Answer :-
def POP(Arr): Arr.pop() return Arr stack = [ ] while True : print() print("Enter your choice as per given -") print("1 = For insert data Enter insert ") print("2 = For delete data enter delete ") print("3 = For Exit enter exit ") print() user = input("Enter your choice :- ") if user == "insert" : num = int(input("Enter a number :- ")) stack.append(num) elif user == "delete" : if stack == [ ]: print("UnderFlow") else : stack = POP(stack) else : break print("Now our stack = ",stack)
Output :-
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- insert
Enter a number :- 47
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- insert
Enter a number :- 564
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- insert
Enter a number :- 965
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- insert
Enter a number :- 4858
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- delete
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- delete
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- insert
Enter a number :- 755
Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit
Enter your choice :- exit
Now our stack = [47, 564, 755]
>>>
# Sorry for previous code
ReplyDelete# This is the correct one
def POP(Arr):
if len(S) == 0:
return "Underflow"
else:
e = Arr.pop()
return str(e) + " Succesffully removed from the stack"
S = []
while True:
print("\nMenu \n 1.Push \n 2.Pop \n 3.Exit")
c = int(input("\nEnter your choice: "))
if c == 1:
e = input("\tEnter the element: ")
S.append(e)
elif c == 2:
print(POP(S))
elif c == 3:
print("\nExiting...")
break
else:
print("\n Enter valid choice")
print("Now your Stack is: ",S)
Code is right : )
DeleteHello Pathwalla
ReplyDeleteYour Website Comments doesn't support indentations
Sorry.
Deletesivinath hi
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )