Bird
0
0
DSA Cprogramming

Peek Top Element of Stack in DSA C

Choose your learning style9 modes available
Mental Model
A stack is like a pile of plates where you only see and use the top plate. Peeking means looking at the top plate without removing it.
Analogy: Imagine a stack of books on a table. You can only see and touch the top book. Peeking is just looking at the title of the top book without taking it off.
Top -> [ 3 ] -> [ 2 ] -> [ 1 ] -> null
          ↑
        (top)
Dry Run Walkthrough
Input: stack: push 1, push 2, push 3; peek top element
Goal: To see the top element of the stack without removing it
Step 1: push 1 onto empty stack
Top -> [ 1 ] -> null
          ↑
Why: We start by adding the first element, which becomes the top
Step 2: push 2 onto stack
Top -> [ 2 ] -> [ 1 ] -> null
          ↑
Why: New element goes on top, pushing previous top down
Step 3: push 3 onto stack
Top -> [ 3 ] -> [ 2 ] -> [ 1 ] -> null
          ↑
Why: Again, new element becomes the top
Step 4: peek top element
Top -> [ 3 ] -> [ 2 ] -> [ 1 ] -> null
          ↑
Why: Peeking shows the top element without changing the stack
Result:
Top -> [ 3 ] -> [ 2 ] -> [ 1 ] -> null
Top element is 3
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for stack
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Push element onto stack
void push(Node** top_ref, int data) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = *top_ref;
    *top_ref = new_node;
}

// Peek top element without removing
int peek(Node* top) {
    if (top == NULL) {
        printf("Stack is empty\n");
        return -1; // Indicate empty stack
    }
    return top->data;
}

// Print stack elements
void printStack(Node* top) {
    Node* current = top;
    printf("Stack from top to bottom: ");
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    Node* stack = NULL;
    push(&stack, 1);
    push(&stack, 2);
    push(&stack, 3);
    printStack(stack);
    int top_element = peek(stack);
    if (top_element != -1) {
        printf("Top element is %d\n", top_element);
    }
    return 0;
}
new_node->next = *top_ref;
link new node to current top to keep stack order
*top_ref = new_node;
update top pointer to new node
if (top == NULL) {
check if stack is empty before peeking
return top->data;
return data of top element without removing
OutputSuccess
Stack from top to bottom: 3 2 1 Top element is 3
Complexity Analysis
Time: O(1) because peeking only reads the top element without traversal
Space: O(n) for n elements in stack due to linked nodes
vs Alternative: Compared to array-based stack, peek is also O(1), but linked list allows dynamic size without resizing
Edge Cases
empty stack
peek prints 'Stack is empty' and returns -1
DSA C
if (top == NULL) {
When to Use This Pattern
When you need to see the last added item without removing it, use the peek operation on a stack.
Common Mistakes
Mistake: Removing the top element instead of just peeking
Fix: Return the top element's data without changing the top pointer
Mistake: Not checking if stack is empty before peeking
Fix: Add a check for NULL top pointer before accessing data
Summary
Peek returns the top element of the stack without removing it.
Use peek when you want to know the current top value but keep the stack unchanged.
The key is to access the top node's data without modifying the stack structure.