Bird
Raised Fist0
DSA Cprogramming~10 mins

Push Operation on Stack in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Push Operation on Stack
Create new node with value
↓
Check if stack is empty?
↓
Set new node as top
↓
Update top pointer to new node
↓
Done
The push operation creates a new node, links it to the current top if any, then updates the top pointer to the new node.
Execution Sample
DSA C
struct Node {
  int data;
  struct Node* next;
};

void push(struct Node** top, int val) {
  struct Node* newNode = malloc(sizeof(struct Node));
  newNode->data = val;
  newNode->next = *top;
  *top = newNode;
}
This code adds a new element to the top of the stack by creating a new node and updating pointers.
Execution Table
StepOperationNodes in StackPointer ChangesVisual State
1Start with empty stackNonetop = NULLtop -> NULL
2Create new node with value 10Node(10)newNode createdtop -> NULL, newNode(10)
3Set newNode.next to top (NULL)Node(10)newNode->next = NULLtop -> NULL, newNode(10)->NULL
4Update top to newNodeNode(10)top = newNodetop -> Node(10) -> NULL
5Create new node with value 20Node(20), Node(10)newNode createdtop -> Node(10) -> NULL, newNode(20)
6Set newNode.next to top (Node(10))Node(20), Node(10)newNode->next = toptop -> Node(10) -> NULL, newNode(20) -> Node(10) -> NULL
7Update top to newNodeNode(20), Node(10)top = newNodetop -> Node(20) -> Node(10) -> NULL
8Create new node with value 30Node(30), Node(20), Node(10)newNode createdtop -> Node(20) -> Node(10) -> NULL, newNode(30)
9Set newNode.next to top (Node(20))Node(30), Node(20), Node(10)newNode->next = toptop -> Node(20) -> Node(10) -> NULL, newNode(30) -> Node(20) -> Node(10) -> NULL
10Update top to newNodeNode(30), Node(20), Node(10)top = newNodetop -> Node(30) -> Node(20) -> Node(10) -> NULL
11End of push operationsNode(30), Node(20), Node(10)top points to Node(30)top -> Node(30) -> Node(20) -> Node(10) -> NULL
💡 All push operations completed, stack top updated accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8Final
topNULLNode(10)Node(20)Node(30)Node(30)
newNodeN/ANode(10)Node(20)Node(30)Node(30)
Key Moments - 3 Insights
Why do we set newNode->next to the current top before updating top?
Because newNode must point to the previous top node to keep the stack linked. This is shown in steps 3, 6, and 9 where newNode->next is assigned before top is updated in steps 4, 7, and 10.
What happens if the stack is empty before push?
If the stack is empty (top is NULL), newNode->next is set to NULL (step 3), and then top is updated to newNode (step 4), making the new node the only element.
How does the visual state show the stack growing?
The visual state shows the top pointer moving to the new node each time, with the new node pointing to the previous top, forming a chain that grows at the front (steps 4, 7, 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 7, what does the top pointer point to?
ANode with value 10
BNode with value 30
CNode with value 20
DNULL
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 7.
At which step does the stack first become non-empty?
AStep 2
BStep 4
CStep 1
DStep 3
💡 Hint
Look for when top changes from NULL to a node in the 'Pointer Changes' column.
If we push a new value 40 after step 10, what will newNode->next point to?
ANode with value 30
BNULL
CNode with value 20
DNode with value 10
💡 Hint
Refer to the pattern in steps 3, 6, and 9 where newNode->next points to the current top.
Concept Snapshot
Push Operation on Stack:
- Create a new node with the value.
- Set new node's next pointer to current top.
- Update top pointer to new node.
- Stack grows by adding nodes at the front.
- Works even if stack is empty (top is NULL).
Full Transcript
The push operation on a stack adds a new element at the top. First, a new node is created with the given value. If the stack is empty, the new node's next pointer is set to NULL. Otherwise, it points to the current top node. Then, the top pointer is updated to this new node, making it the new top of the stack. This process repeats for each push, growing the stack by linking new nodes at the front. The execution table shows each step, including pointer changes and the visual linked list state. The variable tracker follows the top pointer and newNode across steps. Key moments clarify why newNode->next is set before updating top, how empty stack is handled, and how the visual state reflects stack growth. The visual quiz tests understanding of pointer updates and stack state changes during push operations.

Practice

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

Solution

  1. Step 1: Understand stack operations

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

    The push operation adds a new element on top of the existing elements in the stack.
  3. Final Answer:

    Adds an element to the top of the stack -> Option B
  4. Quick Check:

    Push means add on top = C [OK]
Hint: Push always adds on top of stack [OK]
Common Mistakes:
  • Confusing push with pop operation
  • Thinking push removes elements
  • Mixing stack with queue operations
2. Which of the following is the correct syntax to push an element 5 onto a stack named s in C?
easy
A. s.push = 5;
B. s.push(5);
C. push(5, s);
D. push(s, 5);

Solution

  1. Step 1: Understand typical C stack push syntax

    In C, stack operations are usually functions where the stack is passed as the first argument.
  2. Step 2: Match syntax with parameters

    The correct function call to push 5 onto stack s is push(s, 5);.
  3. Final Answer:

    push(s, 5); -> Option D
  4. Quick Check:

    Function push(stack, value) = A [OK]
Hint: In C, function push(stack, value) is standard [OK]
Common Mistakes:
  • Using object.method syntax (not C style)
  • Swapping argument order
  • Assigning value instead of calling function
3. What will be the content of the stack after executing the following code?
int stack[5];
int top = -1;
// Push function
void push(int val) {
  if(top < 4) {
    top++;
    stack[top] = val;
  }
}

push(10);
push(20);
push(30);
medium
A. [10, 20, 30]
B. [30, 20, 10]
C. [10, 20]
D. Empty stack

Solution

  1. Step 1: Trace each push call

    Initially, top = -1. After push(10), top=0, stack[0]=10. After push(20), top=1, stack[1]=20. After push(30), top=2, stack[2]=30.
  2. Step 2: List stack content from bottom to top

    Stack elements from index 0 to top (2) are [10, 20, 30].
  3. Final Answer:

    [10, 20, 30] -> Option A
  4. Quick Check:

    Push adds at top increasing index = B [OK]
Hint: Stack stores pushed elements in order at increasing top index [OK]
Common Mistakes:
  • Reversing order of elements
  • Confusing top index with element values
  • Assuming stack is empty after pushes
4. Identify the error in the following push function for a stack of size 3:
int stack[3];
int top = 0;
void push(int val) {
  if(top <= 3) {
    stack[top] = val;
    top++;
  }
}
medium
A. Condition should be top < 3, not top <= 3
B. top should be incremented before assignment
C. stack array size should be 4
D. top should start at -1, not 0

Solution

  1. Step 1: Analyze top and condition

    top starts at 0, which is valid for empty stack at index 0. The condition allows top to be 3, which is out of bounds for stack[3] (indices 0 to 2).
  2. Step 2: Correct the condition

    Change condition to top < 3 to prevent overflow and invalid access.
  3. Final Answer:

    Condition should be top < 3, not top <= 3 -> Option A
  4. Quick Check:

    Stack max index 2 means top < 3 [OK]
Hint: Check array bounds: top must be less than size [OK]
Common Mistakes:
  • Allowing top to equal array size causing overflow
  • Starting top at -1 but not adjusting code
  • Incrementing top before assignment causing skip
5. Given a stack implemented with an array of size 4 and top = 2 currently, what will happen if we try to push two more elements without popping?
Choose the correct behavior.
hard
A. Both pushes fail immediately due to full stack
B. Both pushes succeed, stack size automatically increases
C. First push succeeds, second push causes stack overflow
D. First push causes overflow, second push is not attempted

Solution

  1. Step 1: Understand current stack state

    Array size is 4, so valid indices are 0 to 3. top=2 means top element is at index 2, so one slot remains at index 3.
  2. Step 2: Push two elements

    First push increments top to 3 and stores element at index 3 (valid). Second push tries to increment top to 4, which is out of bounds, causing overflow.
  3. Final Answer:

    First push succeeds, second push causes stack overflow -> Option C
  4. Quick Check:

    Array size 4, top max index 3, overflow after that [OK]
Hint: Stack size fixed; overflow after last index [OK]
Common Mistakes:
  • Assuming stack auto-expands
  • Thinking both pushes fail immediately
  • Confusing top index with count
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