Skip to content
Laviyx Learn

Try

DS Lab · Experiment 4

Stack using an array

CStacks and queuesJNTUH R22 · CS306PC

Aim

To write a menu-driven C program that implements a stack using an array, with the operations push, pop, peek and display, and reports stack overflow and underflow.

Algorithm

  1. Declare an array stack[MAX] and an integer top = -1. top is the index of the top element; −1 means the stack is empty.
  2. Push x: if top == MAX - 1, report overflow. Otherwise increase top and store x in stack[top].
  3. Pop: if top == -1, report underflow. Otherwise take stack[top] and decrease top.
  4. Peek: if the stack is empty, report it. Otherwise show stack[top] without removing it.
  5. Display: print the elements from stack[top] down to stack[0].
  6. Repeat the menu until the user chooses Exit.

Program

C
#include <stdio.h>

#define MAX 3          /* kept small so that overflow shows quickly */

int stack[MAX];
int top = -1;          /* -1 means the stack is empty */

void push(int value) {
    if (top == MAX - 1) {
        printf("Stack overflow, cannot push %d\n", value);
        return;
    }
    top++;
    stack[top] = value;
    printf("Pushed %d\n", value);
}

void pop(void) {
    if (top == -1) {
        printf("Stack underflow, nothing to pop\n");
        return;
    }
    printf("Popped %d\n", stack[top]);
    top--;
}

void peek(void) {
    if (top == -1) {
        printf("Stack is empty\n");
    } else {
        printf("Top element is %d\n", stack[top]);
    }
}

void display(void) {
    if (top == -1) {
        printf("Stack is empty\n");
        return;
    }
    printf("Stack (top to bottom): ");
    for (int i = top; i >= 0; i--) {
        printf("%d ", stack[i]);
    }
    printf("\n");
}

int main(void) {
    int choice, value;

    printf("STACK USING ARRAY (size %d)\n", MAX);
    printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");

    do {
        printf("\nEnter your choice: ");
        if (scanf("%d", &choice) != 1) {
            printf("Invalid input\n");
            break;
        }
        switch (choice) {
        case 1:
            printf("Enter value to push: ");
            if (scanf("%d", &value) == 1) {
                push(value);
            }
            break;
        case 2:
            pop();
            break;
        case 3:
            peek();
            break;
        case 4:
            display();
            break;
        case 5:
            printf("Exiting.\n");
            break;
        default:
            printf("Invalid choice\n");
        }
    } while (choice != 5);
    return 0;
}

Output

Output
STACK USING ARRAY (size 3)
1. Push
2. Pop
3. Peek
4. Display
5. Exit

Enter your choice: 2
Stack underflow, nothing to pop

Enter your choice: 1
Enter value to push: 10
Pushed 10

Enter your choice: 1
Enter value to push: 20
Pushed 20

Enter your choice: 1
Enter value to push: 30
Pushed 30

Enter your choice: 1
Enter value to push: 40
Stack overflow, cannot push 40

Enter your choice: 4
Stack (top to bottom): 30 20 10 

Enter your choice: 3
Top element is 30

Enter your choice: 2
Popped 30

Enter your choice: 4
Stack (top to bottom): 20 10 

Enter your choice: 5
Exiting.

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

MAX is set to 3 only so that overflow appears in a short run; a real program would use a larger size. Every operation is O(1) because it only looks at stack[top].

Pop does not erase the value from the array. It only moves top down, and the old value is overwritten by the next push.

Common mistakes

  • Checking overflow with top == MAX instead of top == MAX - 1. The last valid index is MAX - 1, so the wrong check lets the program write outside the array.

  • Writing stack[++top] and stack[top--] without understanding the order. Pre-increment for push (move up, then store) and post-decrement for pop (read, then move down) are both needed.

  • Starting top at 0 instead of −1, so the first element is stored at index 1 and the empty check is wrong.

  • Not checking for underflow before popping, which reads stack[-1].

Viva questions

Try to answer each one aloud before you open it.

  • What is a stack?

    A stack is a linear data structure where insertion and deletion happen at the same end, called the top. The last element pushed is the first one popped, so it is called LIFO (Last In, First Out).

  • What are stack overflow and stack underflow?

    Overflow is trying to push onto a full stack. Underflow is trying to pop from an empty stack.

  • What is the time complexity of push and pop?

    Both are O(1), because they only change top and one array element.

  • What is the main limitation of an array stack?

    Its size is fixed when the array is declared. The linked list version can grow as long as memory is available.

  • Give some applications of stacks.

    Function calls and recursion, undo in editors, checking balanced brackets, converting infix to postfix and evaluating postfix expressions.

Learn the concepts

All Data Structures Lab experiments →