(ii) Pop_element(): It pops the objects from the stack and displays them. Also, the function should display “Stack Empty” when there are no elements in the stack.

For example: If the nested list contains the following data:

BList=[["The Alchemist", "Paulo Coelho", 250],

["Animal Farm", "George Orwell", 150],

["The Da Vinci Code", "Dan Brown", 350],

["Harry Potter", "J.K. Rowling", 400],

["The Kite Runner", "Khaled Hosseini", 270],

["The Catcher in the Rye", "J.D. Salinger", 200]]

The stack should contain:

['The Alchemist', 'Paulo Coelho'], 

['Animal Farm', 'George Orwell'], 

['The Catcher in the Rye', 'J.D. Salinger']

The output should be:

['The Catcher in the Rye', 'J.D. Salinger']

['Animal Farm', 'George Orwell']

['The Alchemist', 'Paulo Coelho']

Stack Empty

Answer:

Answer by student

[SQP] Pop_element(): It pops the objects from the stack and displays - Teachoo Sample Paper - Computer Science Class 12

Detailed answer by teachoo

-lock-

The code defines a function named Pop_element that takes no parameters and pops and displays the elements from the stack until it is empty. The function performs the following steps:

    • It loops until the books list (which represents the stack) is empty using a while loop. The condition for this loop is simply books, which evaluates to True if books is not empty and False if books is empty.
    • It pops and displays the top element of the stack using the pop and print methods . The pop method removes and returns the last element of the books list, which represents the top of the stack. The print method displays this element on the screen.
    • After the loop ends, it displays “Stack Empty” using the print method . This indicates that there are no more elements in the stack.

The final code is:

part 2 - Question 30 (ii) - Teachoo Sample Paper - Computer Science Class 12 - Solutions to CBSE Sample Paper - Computer Science Class 12 - Computer Science - Class 12

-endlock-

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.