Bird
Raised Fist0
DSA C++programming~10 mins

Peek Top Element of Stack in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Peek Top Element of Stack
Start
↓
Check if stack is empty?
Yes→Return error or null
No↓
Access top element
↓
Return top element
↓
End
The flow checks if the stack is empty. If not, it accesses and returns the top element without removing it.
Execution Sample
DSA C++
#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> s;
    s.push(10);
    s.push(20);
    int top = s.top();
    cout << top;
    return 0;
}
This code pushes 10 and 20 onto the stack, then peeks the top element (20) and prints it.
Execution Table
StepOperationNodes in StackPointer ChangesVisual State
1Initialize empty stack[]head = nullnull
2Push 10[10]head -> 1010 -> null
3Push 20[20, 10]head -> 2020 -> 10 -> null
4Peek top element[20, 10]head -> 2020 -> 10 -> null
5Return top element value20head unchanged20 -> 10 -> null
6End[20, 10]head -> 2020 -> 10 -> null
💡 Peek stops after returning the top element without modifying the stack.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
stack contents[][10][20, 10][20, 10][20, 10]
head pointernullpoints to 10points to 20points to 20points to 20
top element valueundefinedundefinedundefined2020
Key Moments - 3 Insights
Why does peek not remove the top element from the stack?
Peek only accesses the top element without changing the stack, as shown in execution_table step 5 where the stack remains [20, 10].
What happens if we peek when the stack is empty?
The flow in concept_flow shows that if the stack is empty, peek returns an error or null, preventing access to a non-existent element.
Why does the head pointer not change during peek?
Because peek only reads the top element, the head pointer remains pointing to the same node, as seen in variable_tracker after step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the visual state of the stack?
A10 -> 20 -> null
B20 -> 10 -> null
Cnull
D20 -> null
💡 Hint
Check the 'Visual State' column at step 3 in the execution_table.
At which step does the top element value become defined?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at variable_tracker row 'top element value' to see when it first gets a value.
If the stack was empty, what would the peek operation do according to concept_flow?
AReturn the last pushed element
BAdd a new element
CReturn error or null
DRemove the top element
💡 Hint
Refer to the decision branch in concept_flow for empty stack check.
Concept Snapshot
Peek Top Element of Stack:
- Check if stack is empty
- If not empty, access top element
- Return top element without removing
- Stack remains unchanged
- Useful to see current top value
Full Transcript
Peeking the top element of a stack means looking at the last item added without removing it. The process starts by checking if the stack is empty. If it is, peek returns an error or null to avoid invalid access. If not empty, peek accesses the top element and returns its value. The stack's structure and pointers do not change during peek. This is shown in the execution where after pushing 10 and 20, peeking returns 20 while the stack remains 20 -> 10 -> null.

Practice

(1/5)
1. What does the peek() operation do in a stack?
easy
A. Returns the top element without removing it
B. Removes the top element from the stack
C. Adds a new element to the top of the stack
D. Checks if the stack is empty

Solution

  1. Step 1: Understand stack operations

    A stack is a collection where elements are added or removed from the top only.
  2. Step 2: Define peek operation

    The peek operation returns the top element without removing it, unlike pop which removes it.
  3. Final Answer:

    Returns the top element without removing it -> Option A
  4. Quick Check:

    peek() = top element [OK]
Hint: Peek shows top without removal [OK]
Common Mistakes:
  • Confusing peek with pop
  • Thinking peek adds elements
  • Assuming peek checks emptiness
2. Which of the following is the correct syntax to peek the top element of a stack named stk in C++ STL?
easy
A. stk.pop();
B. stk.peek();
C. stk.top();
D. stk.getTop();

Solution

  1. Step 1: Recall C++ STL stack methods

    The standard method to access the top element is top().
  2. Step 2: Identify correct syntax

    stk.top(); returns the top element without removing it.
  3. Final Answer:

    stk.top(); -> Option C
  4. Quick Check:

    stk.top() = peek top element [OK]
Hint: Use .top() to peek in C++ STL stack [OK]
Common Mistakes:
  • Using .peek() which is not in C++ STL
  • Using .pop() which removes element
  • Using non-existent .getTop() method
3. What will be the output of the following C++ code?
std::stack<int> stk;
stk.push(10);
stk.push(20);
stk.push(30);
std::cout << stk.top() << std::endl;
medium
A. 30
B. 10
C. 20
D. Compilation error

Solution

  1. Step 1: Trace push operations

    Elements pushed in order: 10, then 20, then 30. Stack top is last pushed element 30.
  2. Step 2: Check top() output

    stk.top() returns 30 without removing it.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Last pushed = top = 30 [OK]
Hint: Top is last pushed element [OK]
Common Mistakes:
  • Confusing top with bottom element
  • Assuming top removes element
  • Thinking output is 20 or 10
4. Identify the error in the following code snippet that tries to peek the top element of a stack named stk:
int topElement = stk.peek();
medium
A. stk should be declared as a queue
B. peek() is not a valid method in C++ STL stack
C. topElement should be a pointer
D. stk.pop() should be used instead

Solution

  1. Step 1: Check method availability in C++ STL stack

    C++ STL stack does not have a method named peek().
  2. Step 2: Correct method to peek top element

    The correct method is top(), so stk.top() should be used.
  3. Final Answer:

    peek() is not a valid method in C++ STL stack -> Option B
  4. Quick Check:

    Use stk.top() not stk.peek() [OK]
Hint: Use .top(), not .peek() in C++ STL [OK]
Common Mistakes:
  • Using peek() method which doesn't exist
  • Confusing pop() with peek()
  • Thinking topElement must be pointer
5. Given a stack stk with elements [5, 10, 15] pushed in that order, which code snippet correctly prints the top element without removing it and then prints the stack size?
hard
A. std::cout << stk.pop() << std::endl; std::cout << stk.size() << std::endl;
B. std::cout << stk.top() << std::endl; stk.pop(); std::cout << stk.size() << std::endl;
C. std::cout << stk.peek() << std::endl; std::cout << stk.size() << std::endl;
D. std::cout << stk.top() << std::endl; std::cout << stk.size() << std::endl;

Solution

  1. Step 1: Understand peek without removal

    To peek, use stk.top() which returns top element without removing it.
  2. Step 2: Check stack size after peek

    Since peek does not remove, stk.size() remains 3.
  3. Final Answer:

    std::cout << stk.top() << std::endl; std::cout << stk.size() << std::endl; -> Option D
  4. Quick Check:

    top() no removal + size() correct [OK]
Hint: Use top() to peek, size() unchanged [OK]
Common Mistakes:
  • Using pop() which removes element
  • Using peek() which is invalid in C++ STL
  • Printing size after pop() expecting no change
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard