Chapter 9: Stack Class 12 Computer Science NCERT Solutions

Stacks are a vital data structure in programming, often used for solving problems involving reverse order, undo operations, expression evaluation, and more. In Chapter 9 – Stack, students learn about the LIFO (Last-In, First-Out) concept and how stacks are implemented using arrays in C++.

What You Will Learn in Chapter 9

This chapter builds upon your understanding of arrays and teaches how they can be used to implement stack operations such as push, pop, and peek. You\’ll also explore stack applications like reversing arrays, checking balanced parentheses, and converting expressions.

Key Topics Covered

Introduction to Stack

A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. The last element inserted is the first to be removed.

Stack Terminology

  • Push: Insert an element on top of the stack.
  • Pop: Remove the top element from the stack.
  • Peek/Top: View the topmost element without removing it.
  • Overflow: When trying to push into a full stack.
  • Underflow: When trying to pop from an empty stack.

Stack Implementation Using Arrays

Declaration

int stack[SIZE];
int top = -1;

Push Operation

if (top == SIZE - 1) {
    // Stack Overflow
} else {
    top++;
    stack[top] = value;
}

Pop Operation

if (top == -1) {
    // Stack Underflow
} else {
    int val = stack[top];
    top--;
}

Peek Operation

if (top != -1) {
    cout << stack[top];
}

Stack Memory Representation

Stacks are implemented using arrays with a top variable tracking the index of the current top element. All operations modify top accordingly.

Applications of Stack

  • Reversing a string or array
  • Checking balanced parentheses
  • Evaluating postfix expressions
  • Backtracking problems
  • Undo features in applications

Sample Stack Programs

  • Reverse an Array using Stack
  • Check Balanced Parentheses
  • Evaluate Postfix Expression
  • Convert Infix to Postfix

Output-Based Questions

Identify the final output of stack operations and trace the value of top. Practice dry-runs of push() and pop() sequences.

Common Errors

  • Forgetting to check for underflow/overflow conditions
  • Incorrectly updating top
  • Not using a return statement in pop() when value is needed

Download Chapter 9 Solutions PDF – Stack

Our PDF includes:

  • Fully solved NCERT questions with clear stack logic
  • Program code with dry-run tables and memory diagrams
  • Common stack-based questions for board practice
  • Function-based stack examples with code tracing

Highlights of Our NCERT Solutions

  • Step-by-step explanation of push, pop, peek
  • Visual representation of stack operations
  • Real exam-style questions and board-level programs
  • Loop tracing for stack operations
  • Flowcharts for stack-based algorithms

Recommended Preparation Tips

  • Understand and memorize the stack operations and conditions
  • Practice writing functions for push(), pop(), and peek()
  • Implement stack-based problems using arrays
  • Dry-run push/pop operations to track index behavior
  • Solve expression evaluation and conversion problems

Additional Study Resources

  • Stack Flashcards: Operation logic and syntax
  • Worksheets: Trace-the-stack, fill-in-the-code exercises
  • MCQs and Error Spotting: Concept clarity boosters
  • Previous Year Board Questions: Solved with stack programs
  • Practice Programs: Infix to postfix, reverse stack, undo simulation

Mastering Chapter 9 – Stack

Stacks are foundational in understanding how memory and control flow work in programs. They are essential in recursion, expression parsing, and implementing undo operations. A solid grasp on stack operations helps build advanced problem-solving skills, making you ready for both board exams and competitive programming.

With consistent coding, logic practice, and dry-running stack programs, students can gain complete confidence in one of the most important data structures in C++.

Class 12 Computer Science (C++) – Chapter 9 Stack

Short Answer Type Questions-I[2 marks each]

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

Item Scanned Operation Stack
2Push 22
13Push 132, 13
+Pop 13 & 2, Evaluate 2+13=15, Push 1515
5Push 515, 5
Pop 5 & 15, Evaluate 15-5=10, Push 1010
6Push 610, 6
3Push 310, 6, 3
/Pop 3 & 6, Evaluate 6/3=2, Push 210, 2
5Push 510, 2, 5
*Pop 5 & 2, Evaluate 2*5=10, Push 1010, 10
<Pop 10 & 10, Evaluate 10<10=False, Push FalseFalse

Result: False

Question 2: Evaluate the postfix expression: 100, 40, 8, /, 20, 10, -, +, *

Item Scanned Operation Stack
100Push 100100
40Push 40100, 40
8Push 8100, 40, 8
/Pop 8 & 40, Evaluate 40/8=5, Push 5100, 5
20Push 20100, 5, 20
10Push 10100, 5, 20, 10
Pop 10 & 20, Evaluate 20-10=10, Push 10100, 5, 10
+Pop 10 & 5, Evaluate 5+10=15, Push 15100, 15
*Pop 15 & 100, Evaluate 100*15=1500, Push 15001500

Result: 1500

Question 3: Evaluate the postfix expression: T, F, NOT, AND, T, OR, F, AND

S.No. Scanned Element Operation Stack
1TruePush TrueTrue
2FalsePush FalseTrue, False
3NOTPop False, Push TrueTrue, True
4ANDPop both, Evaluate True AND True = TrueTrue
5TruePush TrueTrue, True
6ORPop both, Evaluate True OR True = TrueTrue
7FalsePush FalseTrue, False
8ANDPop both, Evaluate True AND False = FalseFalse

Result: False

Question 4: Evaluate the postfix expression: F, T, NOT, AND, F, OR, T, AND

S.No. Scanned Element Operation Stack
1FalsePush FF
2TruePush TF, T
3NOTPop T, Push FF, F
4ANDPop both, Evaluate F AND F = FF
5FalsePush FF, F
6ORPop both, Evaluate F OR F = FF
7TruePush TF, T
8ANDPop both, Evaluate F AND T = FF

Result: False

Question 5: Evaluate the postfix expression: 5, 3, 2, *, 4, 2, /, -, *

Item Operation Stack
5Push 55
3Push 35, 3
2Push 25, 3, 2
*Pop 2 & 3, Evaluate 3*2=6, Push 65, 6
4Push 45, 6, 4
2Push 25, 6, 4, 2
/Pop 2 & 4, Evaluate 4/2=2, Push 25, 6, 2
Pop 2 & 6, Evaluate 6-2=4, Push 45, 4
*Pop 4 & 5, Evaluate 5*4=20, Push 2020

Result: 20

Question 6: Evaluate the postfix expression: False, NOT, True, AND, True, False, OR, AND

Element Scanned Stack Status
FalseFalse
NOTTrue
TrueTrue, True
ANDTrue
TrueTrue, True
FalseTrue, True, False
ORTrue, True
ANDTrue

Result: True

Question 7: Code Analysis

int count = 0, Sum = 0;
Stack * Temp = Top;
while (Temp -> Next != NULL)
{
    count++;
    Sum += Temp -> Data;
    Temp = Temp -> Next;
}
cout << Sum / count;

Answer: This code calculates the average of all the elements in the stack, excluding the last node, since the loop runs while Temp->Next != NULL.

Question 8: Convert the expression ((x * 3 + y * 3 + z * 3) / (x + y + z)) into postfix form

Postfix Expression: x 3 * y 3 * + z 3 * + x y + z + /

Question 9: Evaluate the postfix expression: 45, 45, +, 32, 20, 10, /, -, *

Element Stack Status
4545
4545, 45
+90
3290, 32
2090, 32, 20
1090, 32, 20, 10
/90, 32, 2
90, 30
*2700

Result: 2700

Question 10: Convert the Infix Expression P / Q + (R – T) * U to Postfix

Element Operator Stack Postfix Expression
PP
//P
Q/PQ
++PQ/
(+(PQ/
R+(PQ/R
+(-PQ/R
T+(-PQ/RT
)+PQ/RT-
*+*PQ/RT-
U+*PQ/RT-U

Final Postfix Expression: PQ/RT-U*+

Short Answer Type Questions-II[3 marks each]

Question 1: Write the definition of a member function Pop() in C++, to delete a book from a dynamic stack of TEXTBOOKS considering the following code is already included in the program.

struct TEXTBOOKS {
    char ISBN[20];
    char TITLE[80];
    TEXTBOOKS *Link;
};
class STACK {
    TEXTBOOKS *Top;
public:
    STACK() { Top = NULL; }
    void Push();
    void Pop();
    ~STACK();
};

Answer:

void STACK::Pop() {
    if (Top != NULL) {
        TEXTBOOKS *Temp;
        Temp = Top;
        cout << Top->ISBN << " " << Top->TITLE << " deleted" << endl;
        Top = Top->Link;
        delete Temp;
    } else {
        cout << "Stack Empty" << endl;
    }
}

OR any other correct equivalent function definition.

Question 2: Write the definition of a member function Push() in C++, to add a new book in a dynamic stack of BOOKS considering the following code is already included in the program:

struct BOOKS {
    char ISBN[20];
    char TITLE[80];
    BOOKS *Link;
};
class STACK {
    BOOKS *Top;
public:
    STACK() { Top = NULL; }
    void Push();
    void Pop();
    ~STACK();
};

Answer:

void STACK::Push() {
    BOOKS *Temp;
    Temp = new BOOKS;
    gets(Temp->ISBN);
    gets(Temp->TITLE);
    Temp->Link = Top;
    Top = Temp;
}

OR any other correct equivalent function definition.

Question 3: Convert the expression (A - 5) * 6 + (10 / B) / 2 to its corresponding postfix expression. Also, show the status of the operator stack after each step.

Infix Expression: ((A – 5) * 6 + (10 / B)) / 2

Scanned Element Operator Stack Postfix Expression
((
(((
A((A
((-A
5((-A 5
)(A 5 –
*(* A 5 –
6(* A 5 – 6
++A 5 – 6 *
(+(A 5 – 6 *
10+(A 5 – 6 * 10
/+(/A 5 – 6 * 10
B+(/A 5 – 6 * 10 B
)+A 5 – 6 * 10 B /
/+/A 5 – 6 * 10 B /
2+/A 5 – 6 * 10 B / 2

Final Postfix Expression: A 5 - 6 * 10 B / 2 / +

Long Answer Type Questions[4 marks each]

Question 1: Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents for each step of conversion:
Infix Expression: A / ( B + C ) * D – E

Element Stack Expression
AA
//A
(/(A
B/(AB
+/(+AB
C/(+ABC
)/ABC+
*/*ABC+/
D/*ABC+/D
ABC+/D*
EABC+/D*E
(final)ABC+/D*E-

Final Postfix Expression: ABC+/D*E-

Question 2: Write definition for a function DISPMID(int A[][5], int R, int C) in C++ to display the elements of the middle row and middle column from a 2D array A.

void DISPMID(int A[][5], int R, int C) {
    int midRow = R / 2;
    int midCol = C / 2;
    // Display middle row
    for (int i = 0; i < C; i++) {
        cout << A[midRow][i] << " ";
    }
    cout << endl;
    // Display middle column
    for (int i = 0; i < R; i++) {
        cout << A[i][midCol] << " ";
    }
    cout << endl;
}

Question 3: Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents:
Infix Expression: P / ( Q – R ) * S + T

Element Stack Expression
PP
//P
(/(P
Q/(PQ
/( –PQ
R/( –PQR
)/PQR-
*/*PQR-/
S/*PQR-/S
++PQR-/S*
T+PQR-/S*T
(final)PQR-/S*T+

Final Postfix Expression: PQR-/S*T+

Question 4: Convert the following Infix expression to its equivalent Postfix expression:
Infix Expression: X / Y + U * ( V – W )

Element Stack Postfix
XX
//X
Y/XY
++XY/
U+XY/U
*+*XY/U
(+*(XY/U
V+*(XY/UV
+*(-XY/UV
W+*(-XY/UVW
)+*XY/UVW-
(final)+XY/UVW-*+

Final Postfix Expression: XY/UVW-*+

Question 5: Evaluate the following postfix expression using stack and show the contents after execution of each:
Expression: 470 5 4 ^ 25 / 6 *

Step Symbol Operation Stack
1470Push470
25Push470, 5
34Push470, 5, 4
4^5^4 = 625470, 625
525Push470, 625, 25
6/625 / 25 = 25470, 25
76Push470, 25, 6
8*25 * 6 = 150470, 150

Result: 470, 150

Question 6: Write member functions to perform POP and PUSH operations in a dynamically allocated stack containing objects of the structure Game.

struct Game {
    char Gamename[30];
    int numofplayer;
    Game *next;
};
class Stack {
    Game *Top;
public:
    Stack() { Top = NULL; }
    void Push();
    void Pop();
    void display();
};

void Stack::Push() {
    Game *temp = new Game;
    cout << "Enter Game Name: ";
    gets(temp->Gamename);
    cout << "Enter Number of Players: ";
    cin >> temp->numofplayer;
    temp->next = Top;
    Top = temp;
}

void Stack::Pop() {
    if (Top != NULL) {
        Game *temp = Top;
        cout << temp->Gamename << " Deleted\n";
        Top = Top->next;
        delete temp;
    } else {
        cout << "Stack is empty....";
    }
}

Question 7: Write a function PUSHBOOK() in C++ to perform insert operation on Dynamic Stack, which contains Book_No and Book_Title.

struct NODE {
    int Book_No;
    char Book_Title[20];
    NODE *Next;
};

void PUSHBOOK(NODE *&Top, int Book_No, char BTitle[20]) {
    NODE *temp = new NODE;
    temp->Book_No = Book_No;
    strcpy(temp->Book_Title, BTitle);
    temp->Next = Top;
    Top = temp;
}

Question 8: Write a function POPBOOK() in C++ to perform delete operation from a Dynamic Stack.

struct NODE {
    int Bno;
    char Title[20];
    NODE *Link;
};

NODE* POPBOOK(NODE *Top) {
    if (Top == NULL) {
        cout << "Stack is Empty";
        return NULL;
    } else {
        NODE *temp = Top;
        Top = Top->Link;
        cout << "Deleted Book: " << temp->Title << endl;
        delete temp;
        return Top;
    }
}

Question 9: Write the definition of a member function push() for class Library in C++.

struct book {
    int bookid;
    char bookname[20];
    book *next;
};

class Library {
    book *top;
public:
    Library() { top = NULL; }
    void push();
    void pop();
    void disp();
};

void Library::push() {
    book *nptr = new book;
    cout << "Enter values for bookid and bookname: ";
    cin >> nptr->bookid;
    gets(nptr->bookname);
    nptr->next = top;
    top = nptr;
}

Question 10: Convert the following Infix expression to its equivalent Postfix expression:
Infix Expression: U * V + R / (S – T)

Element Stack Postfix
UU
**U
V*UV
++UV*
R+UV*R
/+/UV*R
(+/(UV*R
S+/(UV*RS
+/(-UV*RS
T+/(-UV*RST
)+/UV*RST-
(final)+UV*RST-/+

Final Postfix Expression: UV*RST-/+