Skip to content
Computer ScienceCBSE Class 12 · Stack

Stack and the LIFO Principle: Definition and Applications (Class 12 Computer Science)

Updated 17 October 2026 · Fact-checked

A stack is a linear data structure in which items are inserted and removed at only one end, called the top. The item inserted last is removed first, so the order is LIFO (Last In, First Out). Typical uses are undo, the browser back button, reversing a string and checking balanced brackets.

Push and pop happen only at the top, so the last item in (30) is the first out; the bottom end is closed.

Illustration drawn for QuestionBuddy.

Understand Stack: LIFO Principle and Applications

A data structure is an organised way of storing data in memory so that the operations you need are easy and quick. A list, a tuple and a dictionary are Python data structures. The CBSE syllabus adds one more to study: the stack.

Stack. A stack is a linear data structure, which means its items sit one after another in a line. Insertion and deletion are allowed at one end only, called the top. The other end is closed. Because the newest item is always at the top, the item that went in last is the first to come out: LIFO, Last In, First Out.

Picture to carry. A pile of plates in a canteen: a clean plate is added to the top, and the plate taken is also from the top. You cannot pull a plate out of the middle without disturbing the pile.

Where a stack is used.

  • Undo in an editor: the last change is cancelled first.
  • Back button of a browser: the last page opened is shown first.
  • Reversing a string or list: push every item, then pop every item; they come out in reverse order.
  • Balanced brackets: push an opening bracket, pop when a closing bracket arrives, and check that they match.
  • Function calls: the function called last finishes first, and control returns to its caller.

Terms you must use correctly.

  • Top: the end where all work happens. In the Python list used for a stack, the top is the last element.
  • Push: insert an item at the top. Pop: remove and return the item at the top.
  • Overflow: pushing into a stack that is already full (only for a fixed-size stack; a Python list grows, so it does not overflow in practice).
  • Underflow: popping from an empty stack.

This page covers the idea and the uses. The operations are written as Python code in the next two topics of this chapter.

Stack: LIFO Principle and Applications: key facts

Stack in one line
  • Linear data structure
  • Insert and delete at one end only
  • That end is the top
  • Order is LIFO (Last In, First Out)
The first item pushed is the last one popped.
Core terms
Pushadd at top
Popremove from top
Peekread top without removing
Overflowpush into a full stack
Underflowpop from an empty stack
Peek is also called top.
Uses of a stack
  • Undo
  • Browser back button
  • Reversing a string
  • Matching brackets
  • Function call return order
Learn at least three with the reason in a few words.
Reverse by stack
  • Push items in order
  • Pop until empty
  • Items come out in reverse order
A string reversed this way: CODE gives EDOC.

How to answer Stack: LIFO Principle and Applications questions

For any question that traces a stack, keep a small vertical or horizontal picture and update it after every operation.

  1. 1Write the stack as a list with the bottom on the left and the top on the right, starting empty unless the question gives items.
  2. 2Process the operations one by one. A push adds at the right end; a pop removes the right end and gives that value.
  3. 3Before every pop, check the stack is not empty; if it is empty the answer for that step is underflow.
  4. 4After the last operation, write the final content and name the top item (the rightmost one).
  5. 5For a "which pop sequence is possible" question, simulate: push until the wanted item is on top, pop it, and repeat; the unwanted order is the one where the needed item is buried.
  6. 6For an application question, name the property of LIFO that matches (last action first, reverse order, nearest match first).

Stack: LIFO Principle and Applications in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Undo uses a stack

Suppose a drawing app saves each change (draw line, fill colour, add text) on a pile, and the Undo button cancels the newest change.

The changes were made in the order: line, fill, text. The user presses Undo twice. Which change remains?

Try it first, then show the solutionSolution
  1. The pile is a stack: line is pushed first, then fill, then text, so text is on top.
  2. Undo pops the top: first text is removed, then fill.

Answer: Only the line remains.

Memory hook: The last thing you did is the first thing undone.

Brackets match from the inside out

Suppose a calculator app must check that the brackets in 3*(4+[5-2]) are balanced.

Show the stack of opening brackets while scanning the expression.

Try it first, then show the solutionSolution
  1. Scan left to right. ( is pushed: stack ( .
  2. [ is pushed: stack ( [ .
  3. ] arrives: top is [, a match, so pop: stack ( .
  4. ) arrives: top is (, a match, so pop: stack is empty.

Answer: Balanced: every closing bracket matched the top, and the stack ended empty.

Memory hook: The newest open bracket must close first, like nested boxes.

Tricks to remember Stack: LIFO Principle and Applications

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • LIFO = plates

    A pile of plates: the last plate placed is the first plate lifted.

    Use it when: Deciding the order in which items leave a stack.

  • Top is the active end

    Everything happens at the top; the bottom is never touched until the stack has emptied.

    Use it when: Answering "where do insertion and deletion take place?"

  • Push in, pop reversed

    Whatever order you push in, the pops come back in the opposite order.

    Use it when: Reverse a string, undo, trace questions.

  • Underflow = under empty

    Underflow is popping when there is nothing under the top: the stack is empty.

    Use it when: Separating underflow from overflow.

Stack: LIFO Principle and Applications: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: LIFO meaning · 1 mark · MCQBoard level

Which principle does a stack follow?
(a) FIFO (b) LIFO (c) LILO (d) Random access

Try it first, then show the solutionSolution
  1. A stack removes the item that entered last.
  2. That is Last In, First Out.

Answer: (b) LIFO

Example 2: Tracing push and pop · 2 marks · Short answerHarder board level

An empty stack receives these operations in order: push 5, push 8, push 3, pop, push 9, pop, pop. Write the contents (bottom to top) after each step and name the item left.

Try it first, then show the solutionSolution
  1. After push 5, 8, 3: [5, 8, 3].
  2. pop removes 3: [5, 8]. push 9: [5, 8, 9].
  3. pop removes 9: [5, 8]. pop removes 8: [5].
  4. The stack ends as [5] with 5 on top.

Answer: [5]; the top and only item is 5.

Example 3: Reversing a string with a stack · 3 marks · Short answerHarder board level

Write a Python program that reverses the string "CODE" using a list as a stack, and state the output.

Try it first, then show the solutionSolution
  1. Push every character: the list becomes ['C', 'O', 'D', 'E'] with E on top. (1 mark)
  2. Pop until the list is empty, joining each popped character. (1 mark)
  3. s = []
    for ch in "CODE":
        s.append(ch)
    r = ""
    while s:
        r += s.pop()
    print(r)
    The pops give E, D, O, C. (1 mark)

Answer: EDOC

Example 4: Balanced or not · 3 marks · Short answerHarder board level

Use a stack to decide whether each expression has balanced brackets: (i) {[()]} (ii) {[(])}. Show the stack at the point of decision.

Try it first, then show the solutionSolution
  1. (i) Push {, [, ( . Then ) matches top ( : pop. ] matches top [ : pop. } matches top { : pop. The stack is empty at the end, so balanced. (1.5 marks)
  2. (ii) Push {, [, ( . Then ] arrives, but the top is (, which is not [ . The brackets cross, so not balanced; stop here. (1.5 marks)

Answer: (i) balanced, (ii) not balanced.

Example 5: Case: browser history · 4 marks · Case-basedHOTS / competency

Suppose a simple browser keeps visited pages on a stack and the Back button pops the top page, showing the page below it. The user opens pages A, B, C, D in that order, presses Back twice, then opens page E.
(i) Which page is shown after the two Back presses? (1 mark)
(ii) Write the stack after E is opened. (1 mark)
(iii) Name the property of a stack that makes Back work this way. (1 mark)
(iv) Which page is shown if Back is pressed now? (1 mark)

Try it first, then show the solutionSolution
  1. (i) The stack is [A, B, C, D]. Two pops remove D and C, leaving [A, B]; B is shown.
  2. (ii) Opening E pushes it: [A, B, E].
  3. (iii) LIFO: the most recently opened page is the first one left.
  4. (iv) Back pops E, so B is shown.

Answer: (i) B (ii) [A, B, E] (iii) LIFO (iv) B

Where students lose marks in Stack: LIFO Principle and Applications

The slips examiners see most often, and what to write instead.

  • Mistake: Saying a stack is FIFO

    FIFO is a queue. In a stack the last item inserted leaves first.

    Wrong working

    A stack is First In First Out.

    Right working

    A stack is Last In First Out (LIFO).

    Do this: Link LIFO to the pile of plates before writing.

    Usually costs: 1 mark

  • Mistake: Popping from the wrong end

    The top is the last element in the list used as a stack; the element at index 0 is the bottom.

    Wrong working

    After push 1, 2, 3 the pop returns 1.

    Right working

    The pop returns 3; the stack becomes [1, 2].

    Do this: Always say "the most recently pushed item" for pop.

    Usually costs: 1 mark

  • Mistake: Confusing overflow and underflow

    Overflow is pushing into a full stack; underflow is popping from an empty one.

    Wrong working

    Underflow occurs when the stack is full.

    Right working

    Underflow occurs when the stack is empty.

    Do this: Remember: underflow, under the top there is nothing left.

    Usually costs: 1 mark

Exam tips

  • Define a stack in one sentence with three keywords: linear, one end, LIFO.
  • Quote the application with its reason: undo works because the last change must be undone first.
  • In trace questions, draw the stack after every operation and underline the top.
  • Do not claim a Python list stack overflows: say overflow applies to a fixed-size stack.
  • Peek and top mean the same; push and pop are the two operations the syllabus names.

Practice questions on Stack: LIFO Principle and Applications

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    A stack works on which principle of data handling?
    (a) First In First Out (b) Last In First Out (c) Sorted order (d) Random order

    Hint

    Think of the pile of plates.

    Answer

    (b) Last In First Out

  2. Q2 · 2 marks · Short answerBoard level

    Name any two applications of a stack.

    Hint

    Think of editors and browsers.

    Answer

    Any two: undo in an editor, browser back button, reversing a string, matching brackets, order of function returns.

  3. Q3 · 2 marks · Short answerHarder board level

    A stack contains [10, 20, 30] (top is 30). The operations push 40, pop, pop, push 50 are performed. Write the final stack and the top item.

    Hint

    Update the list after each step.

    Answer

    push 40: [10, 20, 30, 40]; pop gives 40: [10, 20, 30]; pop gives 30: [10, 20]; push 50: [10, 20, 50]. Final stack [10, 20, 50], top is 50.

  4. Q4 · 2 marks · Short answerHarder board level

    Predict the output:

    s = []
    for w in ["red", "green", "blue"]:
        s.append(w)
    s.pop()
    s.append("pink")
    s.append("red")
    print(s, len(s))
    Hint

    Which word does pop() remove?

    Answer

    ['red', 'green', 'pink', 'red'] 4. The pop removes "blue"; then "pink" and "red" are added at the top.

  5. Q5 · 1 mark · MCQHOTS / competency

    Items A, B, C, D, E are pushed in this order, and pops may be done at any time between pushes. Which of these pop orders is impossible?
    (a) E D C B A (b) A B C D E (c) C B E D A (d) A C E B D

    Hint

    Simulate each order; the item to pop must be on top.

    Answer

    (d) A C E B D. After A, C and E are popped, the stack holds B (below) and D (on top), so the next pop gives D, not B. The other three orders are possible.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.What does LIFO stand for?Show answer

    Last In, First Out.

  2. 2.Where do insertion and deletion happen in a stack?Show answer

    At one end only, the top.

  3. 3.What is the term for removing from an empty stack?Show answer

    Underflow.

  4. 4.Which end of a Python list is the top when the list is used as a stack?Show answer

    The last (right) end.

  5. 5.Is a stack a linear or a non-linear data structure?Show answer

    Linear.

  6. 6.Which real feature of a browser uses a stack?Show answer

    The Back button (the last page opened is shown first).

Stack: LIFO Principle and Applications: frequently asked questions

What is a stack in data structures?

A stack is a linear data structure in which items are added and removed at one end only, the top, so the last item added is the first removed (LIFO).

What is the difference between a stack and a queue?

A stack removes the newest item first (LIFO) and works at one end; a queue removes the oldest item first (FIFO) and adds at one end and removes at the other. Queues are not in the Class 12 syllabus.

Why is the stack called a linear data structure?

Its items are arranged one after another in a sequence, like a list; the restriction is that access is only at the top.

Does the Class 12 syllabus include infix and postfix conversion?

No. The 2026-27 syllabus lists only the stack, the push and pop operations and implementation using a list.