---
title: "Stack definition and basic operations"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:stack-operations-algorithms"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/stack-operations-algorithms
site: BCT Engineering
---

# Stack definition and basic operations

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

- Asked in: 2069 Poush, 1+3 marks
- Video: [Introduction to Stacks](https://www.youtube.com/watch?v=I37kGX-nZEI) — Neso Academy
- 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-operations-algorithms
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define Stack with suitable example. Write down the algorithm for the basic operation that can be performed over the stack.

## Short answer

A stack is a linear LIFO structure (e.g., pile of plates). Push inserts at top, pop removes top, peek returns top. Algorithms: push increments top and stores x; pop returns top element and decrements top; overflow/underflow checks ensure bounds.

## 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).

**Example:** a pile of plates. Push 10, 20, 30 gives stack (bottom to top) `10 20 30`; pop returns 30, leaving `10 20`.

#### Algorithms (array `S[0..MAX-1]`, `top = -1` initially)

```text
PUSH(S, x)
1. if top == MAX - 1:  print "Stack overflow"; exit
2. top = top + 1
3. S[top] = x

POP(S)
1. if top == -1:  print "Stack underflow"; exit
2. x = S[top]
3. top = top - 1
4. return x

PEEK(S)                    /* stacktop */
1. if top == -1:  print "Stack empty"; exit
2. return S[top]

EMPTY(S):  return (top == -1)
FULL(S):   return (top == MAX - 1)
```

#### C code

```c
#define MAX 5
int s[MAX], top = -1;
void push(int x) { if (top == MAX-1) printf("Stack overflow\n"); else s[++top] = x; }
int  pop(void)   { if (top == -1) { printf("Stack underflow\n"); return -1; } return s[top--]; }
```
Every operation takes O(1) time.

## Check yourself

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

1. Push 10, 20, 30 and then pop once. What is returned?
   A. 10
   B. 20
   C. 30
   D. Nothing
   Answer: C. The last pushed element, 30, pops first. (Key point: LIFO principle)

2. In the array stack with top = -1 initially, overflow is detected when:
   A. top == -1
   B. top == MAX - 1
   C. top == MAX
   D. top == 0
   Answer: B. The last valid index is MAX-1 so no more pushes fit when top equals it. (Key point: Overflow condition)

3. What is the correct order in PUSH after checking overflow?
   A. S[top] = x, then top = top + 1
   B. top = top - 1, then S[top] = x
   C. top = top + 1, then S[top] = x
   D. S[0] = x only
   Answer: C. top is incremented first, then the item is stored at the new top. (Key point: PUSH steps)

4. When does POP report underflow?
   A. top == MAX - 1
   B. top == -1
   C. top == 0
   D. top == MAX
   Answer: B. top = -1 means the stack is empty so nothing can be removed. (Key point: Underflow condition)

5. What is the time complexity of push and pop?
   A. O(n)
   B. O(log n)
   C. O(n^2)
   D. O(1)
   Answer: D. Each only changes top and one cell, taking constant time. (Key point: Time complexity)

## Related in Data Structure and Algorithm

- [ADT of stack: value and operator definition](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aadt-stack-value-operator) — Data Structure and Algorithm, chapter 1
- [Data structure and basic operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adata-structure-basic-operations) — Data Structure and Algorithm, chapter 1
- [Linked list and its basic operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alinked-list-and-operations) — Data Structure and Algorithm, chapter 4
- [Linear and circular queue operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alinear-circular-queue-algorithms) — Data Structure and Algorithm, chapter 2
- [Radix sort definition and algorithm](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aradix-sort-algorithm) — Data Structure and Algorithm, chapter 7
- [List and its operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alist-and-operations) — Data Structure and Algorithm, chapter 3

## Related in other subjects

- [Basic forging operations](https://www.bctengineer.com/question.md?key=semester_2%2Fworkshop_technology%3Aforging-operations-explained) — Workshop Technology, chapter 9
- [CCS7 stack; why CCS over in-channel](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Accs7-stack-ccs-preferred) — Telecommunications, chapter 5
- [Basic lathe operations](https://www.bctengineer.com/question.md?key=semester_2%2Fworkshop_technology%3Alathe-operations-explained) — Workshop Technology, chapter 6
- [Lathe operations, taper turning and turning calculations](https://www.bctengineer.com/question.md?key=semester_4%2Fmanufacturing_and_production_processes%3Alathe-operations-taper-turning-numerical) — Manufacturing and Production Processes, chapter 7
