---
title: "Static vs dynamic implementation of stack"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:stack-static-vs-dynamic"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/stack-static-vs-dynamic
site: BCT Engineering
---

# Static vs dynamic implementation of stack

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2070 Bhadra, 1+4 marks
- Video: [3.2 Stack Implementation | Static and Dynamic Implementation](https://www.youtube.com/watch?v=OvEgcKs-5_I) — KnowledgeGATE by Sanchit Sir
- 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-static-vs-dynamic
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define Stack. How does static implementation and dynamic implementation of stack differ? Explain.

## Short answer

A stack is a linear LIFO structure. Static implementation uses a fixed-size array with top index; overflow at MAX-1. Dynamic implementation uses a linked list with top pointing to head node; memory allocates/frees as needed, no fixed overflow but extra pointer per node.

## Answer

A **stack** is a linear data structure in which insertion (push) and deletion (pop) take place only at one end, called the **top**. The last element inserted is the first one removed, so it follows **LIFO** (Last In First Out).

#### Static (array) and dynamic (linked list) implementation

In **static implementation** the stack is stored in an array of fixed size declared at compile time. In **dynamic implementation** the stack is a linked list whose nodes are created with `malloc` when pushed and freed when popped; the pointer `top` points to the first node.

| Static (array) | Dynamic (linked list) |
|---|---|
| Fixed size, set at compile time | Grows and shrinks at run time |
| Overflow when `top == MAX-1` | Overflow only when heap memory is exhausted |
| Memory may be wasted if stack is small | Memory used equals number of items (plus pointer per node) |
| No extra pointer field | Each node needs an extra pointer |
| Contiguous memory | Nodes scattered in memory |
| Simple and fast | Slightly slower (allocation, pointer handling) |
| `top` is an integer index | `top` is a pointer to the top node |

#### Static code

```c
int s[MAX], top = -1;
void push(int x) { if (top == MAX-1) printf("Overflow"); else s[++top] = x; }
int  pop()       { return (top == -1) ? -1 : s[top--]; }
```

#### Dynamic code

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

void push(int x) {
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->info = x;  p->next = top;  top = p;       /* insert at beginning */
}
int pop() {
    if (top == NULL) { printf("Underflow"); return -1; }
    struct node *p = top;  int x = p->info;
    top = top->next;  free(p);                   /* delete first node */
    return x;
}
```

## Check yourself

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

1. What is true about static (array) stack implementation?
   A. Size grows at run time
   B. Size is fixed at compile time
   C. Each node has a pointer
   D. It uses malloc for each push
   Answer: B. An array stack has a fixed size declared at compile time. (Key point: Static size)

2. In dynamic stack implementation, how is push done?
   A. Shift all elements
   B. Create a node with malloc and insert it at the beginning
   C. Increase an array index only
   D. Insert at the end of the list
   Answer: B. A new node is allocated and becomes the new first node pointed to by top. (Key point: Dynamic push)

3. When does overflow occur in a dynamic stack?
   A. top == MAX-1
   B. Only when heap memory is exhausted
   C. When top is NULL
   D. Never, even without memory
   Answer: B. There is no fixed MAX; overflow happens only if memory cannot be allocated. (Key point: Overflow difference)

4. What must be done in pop of a dynamic stack to avoid a memory leak?
   A. Set top = NULL
   B. Move top to the next node and free the old node
   C. Do nothing
   D. Shift elements down
   Answer: B. top = top->next and the removed node is released with free(). (Key point: Dynamic pop)

5. Which is a disadvantage of dynamic implementation?
   A. Fixed size
   B. Extra pointer in every node and slower operations
   C. Cannot grow
   D. Wasted unused memory
   Answer: B. Each node needs a pointer and allocation makes it slightly slower. (Key point: Dynamic cost)

## Related in Data Structure and Algorithm

- [Static vs dynamic implementation of list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-vs-dynamic-list-example) — Data Structure and Algorithm, chapter 3, asked 2×
- [Static vs dynamic list; getnode and freenode](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-dynamic-list-getnode-freenode) — Data Structure and Algorithm, chapter 3
- [Why choose static or dynamic list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Achoose-static-or-dynamic-list) — Data Structure and Algorithm, chapter 3
- [Array list; static or dynamic choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-static-dynamic-reason) — Data Structure and Algorithm, chapter 3
- [Static and dynamic data structures; array list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-dynamic-ds-array-list) — Data Structure and Algorithm, chapter 3
- [Dynamic implementation of queue](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adynamic-queue-linked-algorithm) — Data Structure and Algorithm, chapter 4

## Related in other subjects

- [Static vs dynamic mathematical model](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Astatic-vs-dynamic-math-model) — Simulation and Modeling, chapter 2, asked 3×
- [Static and dynamic hazards and their elimination](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Astatic-dynamic-hazards) — Digital Logic, chapter 3, asked 2×
- [Static versus dynamic binding trade-offs](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-vs-dynamic-binding) — Object Oriented Programming, chapter 7
- [Static and dynamic errors in DAC](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Adac-static-dynamic-errors) — Instrumentation II, chapter 4
