Q. Write add(Book) and delete(Books) methods in Python to add Books and Remove Books considering them to act as append() and pop() operations in Stack.

Answer =

def add(Book) :
    stack.append(Book)
def delete(Book) :
    stack.remove(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 the book name :- ") add(Book) elif user == "delete" : if stack == [ ]: print("UnderFlow") else : Book = input("Enter the Book :-") delete(Book) 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 the book name :- Pathwalla
Now our stack =  ['Pathwalla']

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 the book name :- Computer Portal
Now our stack =  ['Pathwalla', 'Computer 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:- insert
Enter the book name :- Portal
Now our stack =  ['Pathwalla', 'Computer Portal', '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 the Book :-Portal
Now our stack =  ['Pathwalla', 'Computer 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:- exit
>>>

Post a Comment

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

Previous Post Next Post