English

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. - Computer Science (Python)

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:

  1. 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.
  2. 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".
  3. 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

  1. def push_book(BooksStack, new_book):
         BooksStack.append(new_book)
  2. def pop_book(BooksStack):
        if not BooksStack:
           print("Underflow")
        else:
           return(BookStack.pop())
  3. def peep(BooksStack):
       if not BooksStack:
          print("None")
       else:
          print(BookStack[-1])
    
shaalaa.com
  Is there an error in this question or solution?
2024-2025 (March) Board Sample Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×