Q. Write PUSH (Books) and POP (Books) methods, in python to add Books and remove book considering them to act as Push and Pop operations of Stack.


Answer :-

def POP(Books):
      Books.pop()
      return Books

def Push (Book):
      stack.append(Book)

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" :
            Book = input("Enter name of Book :- ")
            Push (Book)
      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 

Enter your choice :- insert
Enter name of Book :- Path

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 name of Book :- Walla

Enter your choice as per given -
1 = For insert data Enter insert 
32 = 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 name of Book :- Portal

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 name of Book :- Express

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 =  ['Path', 'Express']

>>>


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post