MCS-208 · Unit 4

Stacks

Section 2 of 7

Block 2 · Unit 4Stacks13 min · Section 2 of 7

4.2Stack Implementation Using Arrays

Covers IGNOU MCS-208 §4.3, 4.3.1

The simplest way to store a stack is an array plus one integer that remembers where the top is. This page gives the push and pop algorithms, a tested C implementation, and notes on the study material’s program.

The representation

An array items[0..MAXSTACK-1] holds the stack, with the bottom item in items[0]. An integer top holds the index of the top item:

  • empty stack: top == -1;
  • full stack: top == MAXSTACK - 1;
  • number of items: top + 1.

Some books number the array from 1, with top == 0 meaning empty and top == MAXSTACK meaning full; the algorithms in §4.3 use that convention. Both are correct as long as you use one consistently.

Algorithms

The order matters: push moves top before storing; pop reads before moving top. In C this is written compactly as items[++top] = x; and x = items[top--];.

Key takeaways

  • Array stack: items[] plus top, the index of the top item (-1 when empty).
  • Push: check overflow, top++, store. Pop: check underflow, read, top--.
  • All operations are O(1)O(1); the capacity is fixed when the array is declared.
  • Keep one convention for top and make every test match it.

Practice questions

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

Question 1 · multiple choice

An array stack uses top = -1 for empty and has MAXSTACK = 50. What is the value of top when the stack is full?

Show answer

Answer: B. 49

The items occupy indices 0 to 49, so the top item’s index is 49. 50 is the full condition for the “next free cell” convention.

Question 2 · multiple choice

Which statement pair correctly pushes x when top is the index of the top item?

Show answer

Answer: A. top = top + 1; items[top] = x;

Move to the next free cell first, then store. The second option overwrites the current top item (or writes to items[-1] on an empty stack); the third never updates top.

Question 3 · multiple choice

What does this program print?

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

int main(void)
{
    int s[5], top = -1;
    s[++top] = 4;
    s[++top] = 7;
    s[++top] = 9;
    int a = s[top--];
    s[++top] = 2;
    int b = s[top--];
    int c = s[top--];
    printf("%d %d %d top=%d\n", a, b, c, top);
    return 0;
}
Show answer

Answer: C. 9 2 7 top=0

After 4, 7, 9 are pushed, pop gives 9; push 2; pop gives 2; pop gives 7, leaving only 4 at index 0, so top is 0.

Question 4 · multiple choice

In the study material’s algorithm, the stack array is indexed from 1 and tos = 0 means empty. Which test detects overflow?

Show answer

Answer: D. tos >= MAXSTACK

With 1-based indexing the last cell is MAXSTACK, so the stack is full when tos reaches it. tos == MAXSTACK - 1 belongs to 0-based indexing.

Question 5 · multiple choice

What is the main disadvantage of the array implementation of a stack?

Show answer

Answer: D. Its capacity is fixed in advance.

The array size is chosen at compile time (or allocation time), so space may be wasted or the stack may overflow. Push and pop are O(1)O(1).

Question 6 · short answer

Write C functions for push and pop on an integer array stack of size MAX (with top = -1 for empty) that report overflow and underflow.

Show worked answer

int stack[MAX], top = -1; int push(int x) { if (top == MAX - 1) { printf("Stack overflow\n"); return 0; } stack[++top] = x; return 1; } int pop(int *x) { if (top == -1) { printf("Stack underflow\n"); return 0; } *x = stack[top--]; return 1; } Both functions are O(1)O(1). Returning a success flag (and the popped value through a pointer) lets the caller tell an empty stack apart from a stored value such as 0 or −1.

↑ ↓ move↵ openTab switch type