Computer ScienceClass 12Stack

Understanding Stack in Class 12 Computer Science: Concepts & Applications

By ConceptScroll Team · Published on 2 July 2026 · 5 min read

Understanding Stack in Class 12 Computer Science: Concepts & Applications

In Class 12 Computer Science, the Stack is a fundamental data structure that follows the Last In First Out (LIFO) principle. This blog explains the Stack concept, its operations, and applications, helping students grasp this essential topic for exams and programming.

What is a Stack? Definition and Basic Concept

A Stack is a linear data structure used to store elements in a particular order. It follows the Last In First Out (LIFO) principle, meaning the last element inserted is the first one to be removed.

Key points about Stack:

  • Elements are added (pushed) and removed (popped) only from the top.
  • It is like a stack of plates; you add or remove plates only from the top.
  • The two main operations are push (insertion) and pop (deletion).

In Class 12 NCERT Computer Science, understanding Stack is crucial as it forms the basis for many algorithms and programming concepts.

Example:

If you push elements 5, 10, and 15 in that order, popping will remove 15 first, then 10, and finally 5.

Stack Operations: Push, Pop, and Peek Explained

The three fundamental operations on a stack are:

  • Push: Adds an element to the top of the stack.
  • Pop: Removes the top element from the stack.
  • Peek (or Top): Returns the top element without removing it.

How these operations work:

OperationDescriptionExample
PushInsert element at the topStack: [5, 10] → Push 15 → [5, 10, 15]
PopRemove element from the topStack: [5, 10, 15] → Pop → [5, 10]
PeekView top element without removalStack: [5, 10, 15] → Peek → 15

Important notes:

  • Stack overflow occurs if you push an element when the stack is full.
  • Stack underflow occurs if you pop from an empty stack.

These operations are essential for solving problems like expression evaluation and function call management.

Want to test yourself on Stack? Try our free quiz →

Applications of Stack in Computer Science and Daily Life

Stacks are widely used in computer science and everyday scenarios:

  • Expression Evaluation: Stacks help convert and evaluate infix, prefix, and postfix expressions.
  • Function Calls: Programming languages use stacks to manage function calls and recursion.
  • Undo Mechanism: Applications like text editors use stacks to implement undo and redo.
  • Backtracking: Algorithms like maze solving use stacks to remember previous steps.
  • Handling Interrupts: Though not a primary stack application, stacks help in saving states during interrupts.

Example:

In a text editor, when you undo an action, the last change is reversed first, following the LIFO principle of stacks.

Understanding Infix, Prefix, and Postfix Notations Using Stack

Arithmetic expressions can be written in three notations:

  • Infix: Operator between operands (e.g., $x + y$).
  • Prefix (Polish): Operator before operands (e.g., $+xy$).
  • Postfix (Reverse Polish): Operator after operands (e.g., $xy+$).

Stacks are crucial for converting between these notations and evaluating them.

Why use different notations?

  • Infix requires parentheses to specify order.
  • Prefix and postfix notations remove the need for parentheses.

Using stack for conversion:

  • To convert infix to postfix, scan the expression and use a stack to temporarily hold operators.
  • Operators are pushed and popped based on precedence and associativity.

Example: Convert infix expression $A + B * C$ to postfix:

1. Push $+$ and $$ according to precedence. 2. Result: $ABC+$

This process is taught in Class 12 NCERT to help students understand expression parsing.

Stack vs Queue: Key Differences for Class 12 Students

Both Stack and Queue are linear data structures but differ in how elements are added and removed.

FeatureStackQueue
PrincipleLast In First Out (LIFO)First In First Out (FIFO)
InsertionAt the top onlyAt the rear (end)
DeletionFrom the top onlyFrom the front (start)
ExampleUndo operationPrint queue
Use caseExpression evaluationScheduling tasks

Understanding these differences helps Class 12 students choose the right data structure for a problem.

Common Errors and Tips When Working with Stacks

When working with stacks, students often face these issues:

  • Stack Overflow: Trying to push an element into a full stack.
  • Stack Underflow: Trying to pop from an empty stack.
  • Incorrect Order: Not following LIFO causes logic errors.

Tips to avoid mistakes:

  • Always check if the stack is full before pushing.
  • Check if the stack is empty before popping.
  • Use clear variable names like top to track the stack's top position.
  • Practice converting expressions and implementing stack operations.

By mastering these, Class 12 students can confidently apply stacks in programming and exams.

Frequently asked questions

What does LIFO mean in the context of a stack?

LIFO means Last In First Out; the last element added is the first to be removed from the stack.

Can elements be inserted or deleted from the middle of a stack?

No, insertion and deletion in a stack happen only at the top end.

How is a stack used in expression evaluation?

Stacks help convert infix expressions to postfix or prefix and evaluate them efficiently.

What causes stack overflow and underflow?

Stack overflow occurs when pushing onto a full stack; underflow occurs when popping from an empty stack.

Is stack used for handling interrupts in computer systems?

Handling interrupts is not a primary application of stacks, but stacks help save states during interrupts.

Ready to ace this chapter?

Get the full Stack chapter — interactive notes, diagrams, worked solutions, polls and a free practice quiz — in the ConceptScroll app.

Open in ConceptScroll →

Study smarter with ConceptScroll

Daily NCERT-aligned reels, AI doubt solving and chapter quizzes — all free.

Start learning free
#class 12#computer science#data structure#expression evaluation#infix#lifo#ncert#postfix#prefix#stack

Continue reading