Advertisements
Advertisements
Question
You have a stack named BooksStack that contains records of books. Each book record is represented as a list containing book_title, author_name, and publication_year.
Write the following user-defined functions in Python to perform the specified operations on the stack BooksStack:
- push_book(BooksStack, new_book): This function takes the stack BooksStack and a new book record new_book as arguments and pushes the new book record onto the stack.
- pop_book(BooksStack): This function pops the topmost book record from the stack and returns it. If the stack is already empty, the function should display "Underflow".
- peep(BookStack): This function displays the topmost element of the stack without deleting it. If the stack is empty, the function should display 'None'.
Code Writing
Solution
-
def push_book(BooksStack, new_book): BooksStack.append(new_book)
-
def pop_book(BooksStack): if not BooksStack: print("Underflow") else: return(BookStack.pop())
-
def peep(BooksStack): if not BooksStack: print("None") else: print(BookStack[-1])
shaalaa.com
Is there an error in this question or solution?