A linked list removes the fixed-size limit of the array stack. The top of the stack is simply the first node of a singly linked list, so push and pop become “insert at the front” and “delete at the front”, both O(1).
Push: create a node, link it to the old top, make it the new top.
Pop: remember the top node, move tos to the next node, free the old node.
The functions receive Node **tos (the address of the caller’s top pointer) because push and pop must change it.
Array stack versus linked stack
Array stack
Linked stack
Capacity
Fixed by the array size
Grows until memory runs out
Overflow
When top == MAX - 1
Only when malloc returns NULL
Memory per item
Data only (but unused cells are wasted)
Data + one pointer
push / pop / peek
O(1)
O(1) (plus the cost of malloc/free)
Memory layout
Contiguous, cache-friendly
Nodes scattered
Check Your Progress 1, Q2(a). The linked representation is better when the maximum size is not known in advance: it uses only as much memory as the stack needs, and it does not overflow until the computer’s memory is exhausted. The array representation is simpler and faster when a good upper bound is known.
Q2(b). Overflow: pushing onto a stack that has no room (array full, or malloc fails). Underflow: popping or peeking on an empty stack. Both must be checked before changing the stack; underflow is often used deliberately to detect the end of processing.
Key takeaways
Linked stack: top = first node; empty stack: tos == NULL.
Push = insert at the front; pop = delete at the front and free; both O(1).
No fixed capacity; each item costs one extra pointer.
Return a success flag from pop instead of a special value such as NULL or −1.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write the push and pop functions of a linked stack in C, and explain the overflow and underflow conditions.
Show worked answer
int push(Node **tos, int x) { Node *n = malloc(sizeof *n); if (n == NULL) return 0; /* overflow: no memory */ n->data = x; n->next = *tos; *tos = n; return 1;}int pop(Node **tos, int *x) { if (*tos == NULL) return 0; /* underflow: empty stack */ Node *t = *tos; *x = t->data; *tos = t->next; free(t); return 1;}
Overflow happens only when malloc returns NULL (memory exhausted); underflow happens when pop is called on an empty stack (*tos == NULL). Both functions are O(1).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/stacks/stack-implementation-using-linked-lists/ to read this page.