MCS-208 · Unit 4

Stacks

Section 1 of 7

Block 2 · Unit 4Stacks9 min · Section 1 of 7

4.1The Stack ADT

Covers IGNOU MCS-208 §4.0, 4.2

Many everyday piles work in one way only: you put things on top and take things from the top. A stack is the data structure that follows this rule. This page defines it as an abstract data type, independent of how it is stored.

Last in, first out

Operations

OperationWhat it doesIf not possible
create(S)Make S an empty stack—
push(S, x)Put x on the top of SOverflow (no room)
pop(S)Remove and return the top itemUnderflow (stack empty)
peek(S) (or top)Return the top item without removing itUnderflow
is_empty(S)True if S has no items—
is_full(S)True if no more items fit (array implementation)—

Trying to push onto a full stack is overflow; trying to pop from an empty stack is underflow. In every good implementation, push, pop and peek take O(1)O(1) time, because they touch only the top.

Where the name “push” comes from

Canteens often keep trays in a spring-loaded rack. Putting a tray on top pushes the pile down against the spring; taking one off lets the pile pop up a little. Only the top tray is ever reachable.

Key takeaways

  • A stack is a list with insertion and deletion at one end, the top: LIFO.
  • Operations: push, pop, peek, is_empty (and is_full for arrays); all O(1)O(1).
  • Overflow = push onto a full stack; underflow = pop from an empty stack.
  • Items come out in reverse order of insertion.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which order of removal does a stack follow?

Show answer

Answer: B. Last in, first out

The item pushed last is on top, so it is popped first. First in, first out is the rule of a queue.

Question 2 · multiple choice

Starting from an empty stack: push 1, push 2, pop, push 3, push 4, pop, pop. Which item remains on the stack?

Show answer

Answer: A. 1

After push 1, push 2, pop → [1]; push 3, push 4 → [1 3 4]; pop, pop → [1]. The popped items are 2, 4, 3.

Question 3 · multiple choice

What is underflow?

Show answer

Answer: D. Popping from an empty stack

Underflow means there is nothing to remove. Pushing onto a full stack is overflow.

Question 4 · multiple choice

The items A, B, C, D are pushed in this order, but pops may happen at any time between pushes. Which output order is impossible?

Show answer

Answer: C. C A B D

To output C first, A and B must already be on the stack with B above A, so B must come out before A. “C A B D” outputs A before B, which the stack cannot do. “A B C D” is possible by popping after each push.

Question 5 · multiple choice

What is the time complexity of push and pop in a correct stack implementation?

Show answer

Answer: C. O(1)O(1) and O(1)O(1)

Both operations work only on the top; nothing is searched or shifted.

Question 6 · short answer

Define a stack and explain the push and pop operations with an example showing the position of top.

Show worked answer

A stack is an ordered list in which all insertions and deletions are made at one end, the top, so the last item inserted is the first removed (LIFO). Push(x) places x on the top; if the stack is full, overflow occurs. Pop removes and returns the top item; if the stack is empty, underflow occurs. Example (bottom → top, top marked): empty; push 10 → [10] top = 10; push 20 → [10 20] top = 20; push 30 → [10 20 30] top = 30; pop returns 30 → [10 20] top = 20.

↑ ↓ move↵ openTab switch type