‘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.
Stack NCERT Textbook With Solutions Book PDF Free Download
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.
Author | NCERT |
Language | English |
No. of Pages | 14 |
PDF Size | 1.7 MB |
Category | Computer Science |
Source/Credits | ncert.nic.in |
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,*,<
Answer:
ITEM SCANNED | OPERATION | STACK |
2 | PUSH 2 | 2 |
13 | PUSH 13 | 2,13 |
+ | POP 13 and 2 Evaluate 2 + 13 = 15 PUSH 15 | 15 |
5 | PUSH 5 | 15,5 |
– | POP 5 & 15 EVALUATE 15-5 = 10 PUSH 10 | 10 |
6 | PUSH 6 | 10, 6 |
3 | PUSH 3 | 10, 6, 3 |
/ | POP 3 & 6 EVALUATE 6/3= 2 PUSH 2 | 10,2 |
5 | PUSH 5 | 10, 2, 5 |
* | POP 5 & 2 EVALUATE 2*5 = 10 PUSH 10 | 10, 10 |
< | POP 10 & 10 EVALUATE 10<10 = FALSE PUSH FALSE | FALSE |
RESULT = FALSE
Question 2.
Evaluate the following postfix expression : (show status of Stack after each operation)
100,40,8,/,20,10,-,+,*
Answer:
ITEMSCANNED | OPERATION | STACK |
100 | PUSH 100 | 100 |
40 | PUSH 40 | 100,40 |
8 | PUSH 8 | 100,40,8 |
/ | POP 8 POP 40 EVALUATE 40/8 =5 PUSH 5 | 100,5 |
20 | PUSH 20 | 100,5,20 |
10 | PUSH 10 | 100, 5, 20, 10 |
POP 10 POP 20 EVALUATE 20-10 =10 PUSH 10 | 100,5,10 | |
+ | POP 10 POP 5 EVALUATE 10 + 5= 15 PUSH 15 | 100,15 |
* | POP 15 POP 100 EVALUATE 100 * 15 = 1500 PUSH 1500 | 1500 |
Question 3.
Evaluate the following postfix expression. Show the status of stack after execution of each operation separately:
T, F, NOT, AND, T, OR, F, AND
Answer:
S.No. | Scanned Element | Operation | Stack |
1 | True | PUSH True | True |
2 | False | PUSH False | False |
3 | NOTCalculate NOT False | POP FalsePUSH True | TrueTrue, True |
4 | Andcalculate:True AND True | POP True POP TruePUSH True | TrueTrue |
5 | True | PUSH True | True, True |
6 | ORCalculate: True OR True | POP TruePUSH True | TrueTrue |
7 | False | PUSH False | True, False |
8 | ANDCalculate: True AND False | POP FalsePOP True PUSH False | TrueFalse |
Thus the stack will have a False Value
NCERT Class 12 Computer Science Textbook Chapter 3 Stack With Answer PDF Free Download