NCERT SOLUTION
CLASS XIIth
SUBJECT- COMPUTER SCIENCE
CHAPTER‐ 3 STACK
Question 1:
What is a Stack? Explain with an example.
Answer: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
meaning the last element added to the stack will be the first one to be removed. A stack has
two primary operations:
• Push: Adds an element to the top of the stack.
• Pop: Removes the top element from the stack.
Example: Imagine a stack of plates. When you add a new plate, it’s placed on top of the
stack (push). When you need a plate, you take the top one off first (pop). If you add plates in
the order A, B, C, D, then the order of removal will be D, C, B, A.
Question 2:
Explain the stack operations with algorithms for Push and Pop.
Answer:
• Push Operation:
o Algorithm:
1. Check if the stack is full.
2. If the stack is full, output "Stack Overflow" and exit.
3. If the stack is not full, increment the top by 1.
4. Add the new element at the position pointed by the top.
o Pseudo Code:
python
Copy code
Push(Stack, Top, Element):
if Top >= MaxSize - 1:
print("Stack Overflow")
else:
Top = Top + 1
Stack[Top] = Element
• Pop Operation:
o Algorithm:
1. Check if the stack is empty.
2. If the stack is empty, output "Stack Underflow" and exit.
3. If the stack is not empty, remove the element from the position
pointed by the top.
4. Decrement the top by 1.
o Pseudo Code:
python
Copy code
Pop(Stack, Top):
if Top == -1:
print("Stack Underflow")
else:
Element = Stack[Top]
Top = Top - 1
return Element
Question 3:
Write a Python program to implement a stack using a list. Include functions
for push, pop, and display operations.
Answer:
python
Copy code
class Stack:
def __init__(self):
self.stack = []
def push(self, element):
self.stack.append(element)
print(f"{element} pushed to stack")
def pop(self):
if not self.stack:
print("Stack Underflow")
else:
element = self.stack.pop()
print(f"{element} popped from stack")
return element
def display(self):
if not self.stack:
print("Stack is empty")
else:
print("Stack elements are:")
for element in reversed(self.stack):
print(element)
# Example usage:
s = Stack()
s.push(10)
s.push(20)
s.display()
s.pop()
s.display()
Output:
arduino
Copy code
10 pushed to stack
20 pushed to stack
Stack elements are:
20
10
20 popped from stack
Stack elements are:
10
Question 4:
Explain the concept of stack overflow and stack underflow with examples.
Answer:
• Stack Overflow: Stack overflow occurs when you try to push an element onto a stack
that is already full. For example, if a stack has a maximum size of 5, and you attempt
to push a 6th element, a stack overflow will occur.
• Stack Underflow: Stack underflow happens when you try to pop an element from an
empty stack. For example, if you create a stack and immediately try to pop an
element without pushing any, a stack underflow will occur.
Example:
python
Copy code
s = Stack()
s.pop() # This will cause a Stack Underflow since the stack is empty.
Question 5:
Convert the following infix expression to postfix expression using stack: A + B
* (C - D) / E.
Answer: To convert an infix expression to postfix expression using a stack, follow these
steps:
• Infix Expression: A + B * (C - D) / E
• Postfix Expression: A B C D - * E / +
Steps:
1. Start scanning the infix expression from left to right.
2. If an operand is encountered, add it to the postfix expression.
3. If an operator is encountered, pop from the stack until the precedence of the
operator on the stack is less than the current operator, then push the current
operator onto the stack.
4. If a left parenthesis is encountered, push it onto the stack.
5. If a right parenthesis is encountered, pop and output from the stack until a left
parenthesis is encountered. Discard the pair of parentheses.
6. Repeat until the end of the expression, then pop all the operators left in the stack.
Dear Teachers and Students,
Join School of Educators' exclusive WhatsApp, Telegram, and Signal groups for FREE access
to a vast range of educational resources designed to help you achieve 100/100 in exams!
Separate groups for teachers and students are available, packed with valuable content to
boost your performance.
Additionally, benefit from expert tips, practical advice, and study hacks designed to enhance
performance in both CBSE exams and competitive entrance tests.
Don’t miss out—join today and take the first step toward academic excellence!
Join the Teachers and Students
Group by Clicking the Link Below
JOIN OUR
WHATSAPP
GROUPS
FOR FREE EDUCATIONAL
RESOURCES
JOIN SCHOOL OF EDUCATORS WHATSAPP GROUPS
FOR FREE EDUCATIONAL RESOURCES
We are thrilled to introduce the School of Educators WhatsApp Group, a
platform designed exclusively for educators to enhance your teaching & Learning
experience and learning outcomes. Here are some of the key benefits you can
expect from joining our group:
BENEFITS OF SOE WHATSAPP GROUPS
Abundance of Content: Members gain access to an extensive repository of
educational materials tailored to their class level. This includes various formats such
as PDFs, Word files, PowerPoint presentations, lesson plans, worksheets, practical
tips, viva questions, reference books, smart content, curriculum details, syllabus,
marking schemes, exam patterns, and blueprints. This rich assortment of resources
enhances teaching and learning experiences.
Immediate Doubt Resolution: The group facilitates quick clarification of doubts.
Members can seek assistance by sending messages, and experts promptly respond
to queries. This real-time interaction fosters a supportive learning environment
where educators and students can exchange knowledge and address concerns
effectively.
Access to Previous Years' Question Papers and Topper Answers: The group
provides access to previous years' question papers (PYQ) and exemplary answer
scripts of toppers. This resource is invaluable for exam preparation, allowing
individuals to familiarize themselves with the exam format, gain insights into scoring
techniques, and enhance their performance in assessments.
Free and Unlimited Resources: Members enjoy the benefit of accessing an array of
educational resources without any cost restrictions. Whether its study materials,
teaching aids, or assessment tools, the group offers an abundance of resources
tailored to individual needs. This accessibility ensures that educators and students
have ample support in their academic endeavors without financial constraints.
Instant Access to Educational Content: SOE WhatsApp groups are a platform where
teachers can access a wide range of educational content instantly. This includes study
materials, notes, sample papers, reference materials, and relevant links shared by
group members and moderators.
Timely Updates and Reminders: SOE WhatsApp groups serve as a source of timely
updates and reminders about important dates, exam schedules, syllabus changes, and
academic events. Teachers can stay informed and well-prepared for upcoming
assessments and activities.
Interactive Learning Environment: Teachers can engage in discussions, ask questions,
and seek clarifications within the group, creating an interactive learning environment.
This fosters collaboration, peer learning, and knowledge sharing among group
members, enhancing understanding and retention of concepts.
Access to Expert Guidance: SOE WhatsApp groups are moderated by subject matter
experts, teachers, or experienced educators can benefit from their guidance,
expertise, and insights on various academic topics, exam strategies, and study
techniques.
Join the School of Educators WhatsApp Group today and unlock a world of resources,
support, and collaboration to take your teaching to new heights. To join, simply click
on the group links provided below or send a message to +91-95208-77777 expressing
your interest.
Together, let's empower ourselves & Our Students and
inspire the next generation of learners.
Best Regards,
Team
School of Educators
Join School of Educators WhatsApp Groups
You will get Pre- Board Papers PDF, Word file, PPT, Lesson Plan, Worksheet, practical
tips and Viva questions, reference books, smart content, curriculum, syllabus,
marking scheme, toppers answer scripts, revised exam pattern, revised syllabus,
Blue Print etc. here . Join Your Subject / Class WhatsApp Group.
Kindergarten to Class XII (For Teachers Only)
Class 1 Class 2 Class 3
Class 4 Class 5 Class 6
Class 7 Class 8 Class 9
Class 10 Class 11 (Science) Class 11 (Humanities)
Class 11 (Commerce) Class 12 (Science) Class 12 (Humanities)
Class 12 (Commerce) Kindergarten
Subject Wise Secondary and Senior Secondary Groups
(IX & X For Teachers Only)
Secondary Groups (IX & X)
SST Mathematics Science
English Hindi-A IT Code-402
Hindi-B Artificial Intelligence
Senior Secondary Groups (XI & XII For Teachers Only)
Physics Chemistry English
Mathematics Biology Accountancy
Economics BST History
Geography Sociology Hindi Elective
Hindi Core Home Science Sanskrit
Psychology Political Science Painting
Vocal Music Comp. Science IP
Physical Education APP. Mathematics Legal Studies
Entrepreneurship French IT
Artificial Intelligence Librarian (All Classes)
Other Important Groups (For Teachers & Principal’s)
Principal’s Group Teachers Jobs IIT/NEET
Join School of Educators WhatsApp Groups
You will get Pre- Board Papers PDF, Word file, PPT, Lesson Plan, Worksheet, practical
tips and Viva questions, reference books, smart content, curriculum, syllabus,
marking scheme, toppers answer scripts, revised exam pattern, revised syllabus,
Blue Print etc. here . Join Your Subject / Class WhatsApp Group.
Kindergarten to Class XII (For Students Only)
Class 1 Class 2 Class 3
Class 4 Class 5 Class 6
Class 7 Class 8 Class 9
Class 10 Class 11 (Science) Class 11 (Humanities)
Class 11 (Commerce) Class 12 (Science) Class 12 (Humanities)
Class 12 (Commerce) Artificial Intelligence
(VI TO VIII)
Subject Wise Secondary and Senior Secondary Groups
(IX & X For Students Only)
Secondary Groups (IX & X)
SST Mathematics Science
English Hindi IT Code
Artificial Intelligence
Senior Secondary Groups (XI & XII For Students Only)
Physics Chemistry English
Mathematics Biology Accountancy
Economics BST History
Geography Sociology Hindi Elective
Hindi Core Home Science Sanskrit
Psychology Political Science Painting
Music Comp. Science IP
Physical Education APP. Mathematics Legal Studies
Entrepreneurship French IT
AI IIT/NEET CUET
Groups Rules & Regulations:
To maximize the benefits of these WhatsApp groups, follow these guidelines:
1. Share your valuable resources with the group.
2. Help your fellow educators by answering their queries.
3. Watch and engage with shared videos in the group.
4. Distribute WhatsApp group resources among your students.
5. Encourage your colleagues to join these groups.
Additional notes:
1. Avoid posting messages between 9 PM and 7 AM.
2. After sharing resources with students, consider deleting outdated data if necessary.
3. It's a NO Nuisance groups, single nuisance and you will be removed.
No introductions.
No greetings or wish messages.
No personal chats or messages.
No spam. Or voice calls
Share and seek learning resources only.
Please only share and request learning resources. For assistance,
contact the helpline via WhatsApp: +91-95208-77777.
Join Premium WhatsApp Groups
Ultimate Educational Resources!!
Join our premium groups and just Rs. 1000 and gain access to all our exclusive
materials for the entire academic year. Whether you're a student in Class IX, X, XI, or
XII, or a teacher for these grades, Artham Resources provides the ultimate tools to
enhance learning. Pay now to delve into a world of premium educational content!
Click here for more details
Class 9 Class 10
Class 11(Science Stream)
Class 11(Commerce Stream) Class 11 Humanities Class 12 (Science Stream)
(Arts) Stream
Class 12 (Commerce Stream) Class 12Humanities
(Arts) Stream
📣 Don't Miss Out! Elevate your academic journey with top-notch study materials and secure
your path to top scores! Revolutionize your study routine and reach your academic goals with
our comprehensive resources. Join now and set yourself up for success! 📚 🌟
Best Wishes,
Team
School of Educators & Artham Resources
SKILL MODULES BEING OFFERED IN
MIDDLE SCHOOL
Artificial Intelligence Beauty & Wellness Design Thinking & Financial Literacy
Innovation
Handicrafts Information Technology Marketing/Commercial Mass Media - Being Media
Application Literate
Data Science (Class VIII Augmented Reality /
Travel & Tourism Coding
only) Virtual Reality
Digital Citizenship Life Cycle of Medicine & Things you should know What to do when Doctor
Vaccine about keeping Medicines is not around
at home
Humanity & Covid-19 Blue Pottery Pottery Block Printing
Food Food Preservation Baking Herbal Heritage
Khadi Mask Making Mass Media Making of a Graphic
Novel
Kashmiri Embroidery Satellites
Rockets
Embroidery
Application of Photography
Satellites
SKILL SUBJECTS AT SECONDARY LEVEL (CLASSES IX – X)
Retail Information Technology Automotive
Security
Introduction To Financial Introduction To Tourism Beauty & Wellness Agriculture
Markets
Food Production Front Office Operations Banking & Insurance Marketing & Sales
Health Care Apparel Multi Media Multi Skill Foundation
Course
Artificial Intelligence
Physical Activity Trainer Electronics & Hardware
Data Science
(NEW)
Foundation Skills For Sciences Design Thinking & Innovation (NEW)
(Pharmaceutical & Biotechnology)(NEW)
SKILL SUBJECTS AT SR. SEC. LEVEL
(CLASSES XI – XII)
Retail InformationTechnology Web Application Automotive
Financial Markets Management Tourism Beauty & Wellness Agriculture
Food Production Front Office Operations Banking Marketing
Health Care Insurance Horticulture Typography & Comp.
Application
Geospatial Technology Electrical Technology Electronic Technology Multi-Media
Taxation Cost Accounting Office Procedures & Shorthand (English)
Practices
Shorthand (Hindi) Air-Conditioning & Medical Diagnostics Textile Design
Refrigeration
Salesmanship Business Food Nutrition &
Design
Administration Dietetics
Mass Media Studies Library & Information Fashion Studies Applied Mathematics
Science
Yoga Early Childhood Care & Artificial Intelligence Data Science
Education
Physical Activity Land Transportation Electronics & Design Thinking &
Trainer(new) Associate (NEW) Hardware (NEW) Innovation (NEW)
Join School of Educators Signal Groups
You will get Pre- Board Papers PDF, Word file, PPT, Lesson Plan, Worksheet, practical
tips and Viva questions, reference books, smart content, curriculum, syllabus,
marking scheme, toppers answer scripts, revised exam pattern, revised syllabus,
Blue Print etc. here . Join Your Subject / Class signal Group.
Kindergarten to Class XII
Class 2 Class 3
Class 1
Class 5 Class 6
Class 4
Class 7 Class 8 Class 9
Class 10 Class 11 (Science) Class 11 (Humanities)
Class 11 (Commerce) Class 12 (Science) Class 12 (Humanities)
Class 12 (Commerce) Kindergarten Artifical intelligence
Subject Wise Secondary and Senior Secondary
Groups IX & X
Secondary Groups (IX & X)
SST Mathematics Science
Hindi-B
English Hindi-A
IT Artifical intelligence
IT Code-402
Senior Secondary Groups XI & XII
Physics Chemistry English
Biology Accountancy
Mathematics
BST History
Economics
Geography Sociology Hindi Elective
Hindi Core Home Science Sanskrit
Psychology Political Science Painting
IP
Vocal Music Comp. Science
Physical Education APP. Mathematics Legal Studies
IIT/NEET
Entrepreneurship French
Artifical intelligence CUET
Join School of Educators CBSE Telegram Groups
Kindergarten
All classes Class 1 Class 2
Class 3 Class 4 Class 5
Class 6 Class 7 Class 8
Class 9 Class 10 Class 11 (Sci)
Class 11 (Com) Class 11 (Hum) Class 12 (Sci)
Class 12 (Com) Class 12 (Hum) JEE/NEET
CUET NDA, OLYMPIAD, NTSE Principal Professional Group
Teachers Professional Group Project File Group
Join School of Educators ICSE Telegram Groups
Kindergarten Class 1 Class 2
Class 3 Class 4 Class 5
Class 6 Class 7 Class 8
Class 9 Class 10 Class 11 (Sci)
Class 11 (Com) Class 11 (Hum) Class 12 (Sci)
Class 12 (Com) Class 12 (Hum)