MCS-208 · Unit 4

Stacks

Section 3 of 7

Block 2 · Unit 4Stacks11 min · Section 3 of 7

4.3Stack Implementation Using Linked Lists

Covers IGNOU MCS-208 §4.3.2

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)O(1).

The representation

A linked stack after pushing 10, 20, 30
 tos
+---+     +----+---+     +----+---+     +----+------+
| o-+---> | 30 | o-+---> | 20 | o-+---> | 10 | NULL |
+---+     +----+---+     +----+---+     +----+------+
          (top)                          (bottom)
  • Empty stack: tos == NULL.
  • 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 stackLinked stack
CapacityFixed by the array sizeGrows until memory runs out
OverflowWhen top == MAX - 1Only when malloc returns NULL
Memory per itemData only (but unused cells are wasted)Data + one pointer
push / pop / peekO(1)O(1)O(1)O(1) (plus the cost of malloc/free)
Memory layoutContiguous, cache-friendlyNodes 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)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 1 · multiple choice

In a linked stack, where are items pushed and popped?

Show answer

Answer: A. Both at the head (first node) of the list

Using the head for both makes push and pop O(1)O(1). Using the tail of a singly linked list would require walking the list.

Question 2 · multiple choice

When does a linked stack overflow?

Show answer

Answer: C. When malloc cannot allocate a new node

Only running out of memory stops it. tos == NULL means the stack is empty, which is the underflow condition.

Question 3 · multiple choice

What does this program print?

C · q4-linked.c
#include <stdio.h>

typedef struct node {
    int data;
    struct node *next;
} Node;

int main(void)
{
    Node c = {5, NULL}, b = {8, &c}, a = {2, &b};
    Node *tos = &a;
    int sum = 0;
    tos = tos->next;                   /* pop */
    for (Node *p = tos; p != NULL; p = p->next)
        sum += p->data;
    printf("top=%d sum=%d\n", tos->data, sum);
    return 0;
}
Show answer

Answer: B. top=8 sum=13

The top node holds 2; after tos = tos->next the top is 8, and the remaining items are 8 and 5, so the sum is 13. (A real pop would also free the old node; here the nodes are local variables.)

Question 4 · multiple choice

Why should pop not return NULL (or 0) to signal an empty stack of integers?

Show answer

Answer: D. Because 0 may be a real value on the stack, so the caller could not tell “empty” from “popped 0”

Return a separate success flag and pass the value through a pointer. Also, NULL is meant for pointers, not integers.

Question 5 · multiple choice

Which is an advantage of the array stack over the linked stack?

Show answer

Answer: B. It needs no extra pointer per item.

An array stores only the data. Both stacks have O(1)O(1) push and pop; the array stack can overflow and cannot grow.

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)O(1).

↑ ↓ move↵ openTab switch type