Stacks, Queues and Deques Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Stacks, Queues and Deques notes for GATE DA: 23 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Stacks, Queues, and Deques

    Chapter Journey: Linear Data Structures

    01
    Stack Operations and Simulation
    Master Push, Pop, and Top. Learn to trace complex pseudocode executions manually.
    Weightage Hint: High frequency in simulation questions.
    02
    Queue and Deque Operations
    Understand F I F O behavior, circular queues, and double-ended insertion and removal.
    Weightage Hint: Essential for scheduling and buffering concepts.
    03
    ADT Properties: FIFO, LIFO, Lookup
    Formalize the abstract definitions. Match properties to real-world applications.
    Weightage Hint: Conceptual clarity for matching and theory questions.

    The Stack Intuition: The Plate Pile

    What is a Stack?

    A Stack is a linear data structure that follows a particular order in which operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out).

    Real-World Analogy

    Imagine a stack of books on a table:

    1. You place Book A down.
    2. You place Book B on top of A.
    3. You place Book C on top of B.

    To access Book A, you must first remove Book C, then Book B. The last item added (C) is the first one removed.

    Book C
    Book B
    Book A

    Key Terminology

    • Top: The only end where insertion and deletion occur.
    • Push: The operation of adding an element to the top.
    • Pop: The operation of removing the element from the top.

    Core Operations: Push and Pop

    Fundamental Operations

    A stack supports two main atomic operations. In exam simulations, tracking these step-by-step is crucial.

    Operation Description Pre-condition Check Time Complexity
    Push(x) Inserts element at the top. Check for Overflow (Is stack full?)
    Pop() Removes and returns the top element. Check for Underflow (Is stack empty?)
    Peek/Top Returns the top element without removing it. Check for Underflow

    Visualizing the State Change

    Let be a stack. Initially .

    1. Push(10) (Top is 10)
    2. Push(20) (Top is 20)
    3. Pop() Returns 20, (Top is 10)
    Note: In array-based implementations, a pointer top usually tracks the index of the current top element.

    Simulating Pseudocode: Step-by-Step Execution

    Problem Statement

    Consider the following pseudocode:

    Create empty stack S
        Set x=0, flag=0, sum=0
        Push x onto S       // S: [0]
        
        while (S is not empty){
            if (flag equals 0){
                Set x = x+1
                Push x onto S
            }
            if (x equals 8):
                Set flag=1
            if (flag equals 1){
                x = Pop(S)
                if (x is odd):
                    Pop(S)      // Discard this value
                    Set sum = sum + x
            }
        }
        Output sum

    Step-by-Step Trace

    Phase 1: Building the Stack (Flag = 0)

    The loop runs, incrementing x and pushing it until x reaches 8.

    • Init: , , ,
    • Iteration 1: becomes 1, push 1.
    • ...
    • Iteration 8: becomes 8, push 8.
    • Condition check: is true, so becomes 1.
    • Since , we execute the pop block: . . is not odd, so nothing is added to .

    Phase 2: Emptying the Stack (Flag = 1)

    Now . The first if block is skipped. We only evaluate the check and the block.

    Iteration Stack Before x = Pop(S) Is Odd? Action Stack After
    1 Yes Pop (discards 6), 7
    2 Yes Pop (discards 4), 12
    3 Yes Pop (discards 2), 15
    4 Yes Pop (discards 0), 16

    Next iteration: is empty. Loop terminates.

    Final Answer

    The value of is 16

    Stacks, Queues and Deques: Solved Questions with Step-by-Step Explanations (2 Problems)

    Question 1 · Programming, Data Structures and Algorithms MCQ

    In a Stack data structure, the <code>Push</code> operation inserts a new element at which position?

    1. A.

      At the top of the stack

    2. B.

      At the bottom of the stack

    3. C.

      At the middle of the stack

    4. D.

      At a random position

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a definition recall question about the Push operation, recognizable because it asks where a new element is placed in a stack. Step 1: Recall that a stack follows the LIFO (Last In, First Out) principle. Step 2: To maintain LIFO order, new elements must be added at the same end from which they will be removed. This end is called the top. Step 3: Therefore, Push always inserts at the top of the stack. Answer: Option A
    Question 2 · Programming, Data Structures and Algorithms MCQ

    Which stack operation removes and returns the topmost element?

    1. A.

      Push

    2. B.

      Peek

    3. C.

      Pop

    4. D.

      IsEmpty

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a definition recall question about stack operations, recognizable because it asks which operation both removes and returns the top element. Step 1: Review the four standard stack operations. - Push: Adds an element to the top. Does not remove. - Peek (or Top): Returns the top element without removing it. - Pop: Removes the top element and returns it. - IsEmpty: Checks whether the stack is empty. Returns a boolean. Step 2: The question asks for the operation that both removes and returns. Only Pop does both. Answer: Option C

    More notes in this unit

    chapter
    Stacks, Queues and Deques Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Stacks, Queues and Deques notes for GATE DA: 23 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    A question from this chapter

    Question 1

    In a Stack data structure, the <code>Push</code> operation inserts a new element at which position?

    Question 2

    Which stack operation removes and returns the topmost element?

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.