---
title: "Push and pop on stack as linked list"
subject: "Data Structure and Algorithm"
chapter: 4
key: "semester_4/data_structure_and_algorithm:stack-push-pop-linked-list"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/stack-push-pop-linked-list
site: BCT Engineering
---

# Push and pop on stack as linked list

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2073 Bhadra, 5 marks
- Video: [Data Structures: Linked List implementation of stacks](https://www.youtube.com/watch?v=MuwxQ2IB8lQ) — mycodeschool
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/stack-push-pop-linked-list
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

How do you perform a push and pop operation in stack as a linked list?

## Short answer

Push: allocate a new node, assign its next to the current top, and update top to this new node. Pop: store the top node’s info, advance top to the next node, free the old top. Both operations are O(1).

## Answer

A stack as a linked list keeps its **top** at the head of a singly linked list, so push and pop both work at the front. Nodes are created with `malloc`, hence no fixed size limit.

```c
struct node { int info; struct node *next; };
struct node *top = NULL;
```

#### PUSH(x)
1. Create node `p` using malloc; if NULL, print "overflow" and stop.
2. `p->info = x`.
3. `p->next = top` (new node points to the old top).
4. `top = p`.

#### POP()
1. If `top == NULL`, print "underflow" and stop.
2. `p = top`.
3. `x = p->info`.
4. `top = top->next`.
5. `free(p)`; return `x`.

```text
Initial:           push(30):               pop():
top -> [20]->[10]  top -> [30]->[20]->[10]  top -> [20]->[10]  (returns 30)
```

```c
void push(int x) {
    struct node *p = (struct node *)malloc(sizeof(struct node));
    if (!p) { printf("Overflow\n"); return; }
    p->info = x; p->next = top; top = p;
}
int pop(void) {
    if (!top) { printf("Underflow\n"); return -1; }
    struct node *p = top; int x = p->info;
    top = top->next; free(p);
    return x;
}
```
Both operations take O(1) time.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Where is the top of the linked stack placed?
   A. At the head of the list
   B. At the tail
   C. In a separate array
   D. In the middle
   Answer: A. Working at the head makes push and pop O(1). (Key point: Top position)

2. Why is p->next = top done before top = p?
   A. To free memory
   B. So the new node keeps a link to the old stack
   C. To make the stack circular
   D. To check overflow
   Answer: B. Reversing the order would lose the old stack. (Key point: Push order)

3. What does pop do after saving x?
   A. top = NULL
   B. p->next = top
   C. top = top->next and free the old top node
   D. free(top->next)
   Answer: C. The second node becomes top and the removed node is released. (Key point: Pop)

4. When does underflow occur?
   A. malloc returns NULL
   B. The stack has one node
   C. top->next is NULL
   D. top == NULL when pop is called
   Answer: D. Nothing to pop from an empty stack; malloc failure is overflow. (Key point: Underflow)

## Related in Data Structure and Algorithm

- [Stack and queue using linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-queue-singly-linked-list) — Data Structure and Algorithm, chapter 4, asked 2×
- [Linked stack overcoming array demerits](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-linked-overcome-array-demerits) — Data Structure and Algorithm, chapter 4
- [Stack definition and basic operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-operations-algorithms) — Data Structure and Algorithm, chapter 2
- [Insert and delete kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-delete-kth) — Data Structure and Algorithm, chapter 4, asked 2×
- [Delete last node of doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adelete-end-node-doubly) — Data Structure and Algorithm, chapter 4
- [Doubly linked list: bank customer records](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-bank-customer) — Data Structure and Algorithm, chapter 4

## Related in other subjects

- [PC and SP in CALL, RET, PUSH, POP](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Apc-sp-call-ret-push-pop) — Microprocessor, chapter 2
- [Compare CALL/RET with PUSH/POP](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Acall-ret-vs-push-pop) — Microprocessor, chapter 2
- [IRQ, ISR, IVT, PUSH, POP, latency](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Ainterrupt-terms-irq-isr-ivt) — Embedded System, chapter 5
- [Justify EMS expenditure; push-pull SCM](https://www.bctengineer.com/question.md?key=semester_8%2Finformation_system%3Aems-expenditure-push-pull-scm) — Information System, chapter 3
