# Stack NCERT Textbook With Solution PDF

NCERT Solutions for Class 12 Computer Science Chapter 3 Stack‘ PDF Quick download link is given at the bottom of this article. You can see the PDF demo, size of the PDF, page numbers, and direct download Free PDF of ‘Ncert Class 12 Computer Science Chapter 3 Exercise Solution” using the download button.

### Chapter 3: Stack

We have seen piles of books in the library or stacks of plates at home (Figure 3.1). To put another book or another plate in such a pile, we always place (add to the pile) the object at the top only.

Likewise, to remove a book or a plate from such a pile, we always remove (delete from the pile) the object from the top only.

This is because in a large pile, it is inconvenient to add or remove an object from in between or bottom. Such an arrangement of elements in linear order is called a stack.

We add new elements or remove existing elements from the same end, commonly referred to as the top of the stack. It thus follows the Last-In-First-out (LIFO) principle.

That is, the element which was inserted last (the most recent element) will be the first one to be taken out from the stack.

#### 3.2.1 Application of Stack

Some of the applications of the stack in real life are:
• Pile of clothes in an almirah
• Multiple chairs in a vertical pile
• Bangles worn on wrist
• Pile of boxes of eatables in pantry or on a kitchen shelf
Some examples of application of stack in programming are as follows:

• When we need to reverse a string, the string is traversed from the last character till the first
character. i.e. characters are traversed in the reverse order of their appearance in the string. This is very easily done by putting the characters of a string in a stack.

• We use text/image editor for editing the text/image where we have options to redo/undo the editing done. When we click on the redo /undo icon, the most recent editing is redone/undone. In this scenario, the system uses a stack to keep track of changes made.

• While browsing the web, we move from one web page to another by accessing links between them. In order to go back to the last visited web page, we may use the back button on the browser.

Let us say we accessed a web page P1 from where we moved to web page P2 followed by browsing of web page P3. Currently, we are on web page P3 and want to revisit web page P1.

We may go to a previously visited web page by using the BACK button of the browser. On clicking the BACK button once, we are taken from web page P3 to web page P2, another click on BACK shows web page P1. In this case, the history of browsed pages is maintained as a stack.

#### 3.3 Operation On Stack

As explained in the previous section, a stack is a mechanism that implements LIFO arrangement hence elements are added and deleted from the stack at one end only.

The end from which elements are added or deleted is called TOP of the stack. Two fundamental operations performed on the stack are PUSH and POP. In this section, we will learn about them and implement them using Python

#### IMPLEMENTATION OF STACK IN PYTHON

We have learnt so far that a stack is a linear and ordered collection of elements. The simple way to implement a stack in Python is using the data type list.

We can fix either of the sides of the list as TOP to insert/remove elements. It is to be noted that we are using built-in methods append() and pop() of the list for implementation of the stack. As these built-in methods insert/delete elements at the rightmost end of the list, hence explicit declaration of TOP is not needed.

### NCERT Solutions Class 12 Computer Science Chapter 3 Stack

Question 1.
Evaluate the following postfix expression. Show the status of the stack after the execution of each operation separately:
2,13, + , 5, -,6,3,/,5,*,<

RESULT = FALSE

Question 2.
Evaluate the following postfix expression : (show status of Stack after each operation)
100,40,8,/,20,10,-,+,*