📘 Lesson  ·  Lesson 63

Stack using Array

The goal: a working stack program

This is a hands-on practice program. If you want to first understand what a stack is and how LIFO works, read the stack in C concept lesson. Here we go straight to building a complete, menu-driven stack program using an array that you can compile and run.

What the program will do

The program keeps showing a menu and performs whatever the user picks:

  • 1. Push — add a number to the top
  • 2. Pop — remove the top number
  • 3. Peek — see the top number without removing it
  • 4. Display — show all numbers from top to bottom
  • 5. Exit

The complete program

C Language
#include <stdio.h>
#define SIZE 100
int stack[SIZE];
int top = -1;

void push(int x) {
    if (top == SIZE - 1) { printf("Stack full\n"); return; }
    stack[++top] = x;
    printf("%d pushed\n", x);
}
void pop() {
    if (top == -1) { printf("Stack empty\n"); return; }
    printf("%d popped\n", stack[top--]);
}
void peek() {
    if (top == -1) { printf("Stack empty\n"); return; }
    printf("Top = %d\n", stack[top]);
}
void display() {
    if (top == -1) { printf("Stack empty\n"); return; }
    printf("Stack: ");
    for (int i = top; i >= 0; i--) printf("%d ", stack[i]);
    printf("\n");
}
int main() {
    int choice, value;
    while (1) {
        printf("\n1.Push 2.Pop 3.Peek 4.Display 5.Exit: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: printf("Value: "); scanf("%d", &value); push(value); break;
            case 2: pop(); break;
            case 3: peek(); break;
            case 4: display(); break;
            case 5: return 0;
            default: printf("Invalid\n");
        }
    }
}

A sample run

Sample Output:
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 1
Value: 10
10 pushed
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 1
Value: 20
20 pushed
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 4
Stack: 20 10
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 2
20 popped
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 5

Dry run of push and pop

ActiontopStack contents
start-1(empty)
push 10010
push 20110, 20
pop → 20010

Each push moves top up before storing; each pop reads at top then moves it down. The display loop runs from top down to 0.

Common mistakes

  • Starting top at 0 instead of -1 for an empty stack.
  • Forgetting the full/empty checks, causing overflow or reading invalid memory.
  • Displaying from 0 upward instead of top downward (wrong order).
  • Mixing ++top (pre) and top++ (post) incorrectly in push/pop.
🏋️ Practice

Add a "count" option that prints how many items are in the stack (that is just top + 1). Then try converting the fixed array into a dynamically allocated one.

Summary

  • An array stack uses an array plus a top index starting at -1.
  • A menu-driven loop exercises push, pop, peek and display.
  • Push moves top up then stores; pop reads then moves down.
  • Display runs from top down to 0 to show LIFO order.
  • Always guard against full and empty conditions.

Frequently Asked Questions

How do you implement a stack using an array in C?
You use an array to hold the items and an integer top to mark the current top position, starting at -1. push increases top and stores the value; pop returns the value at top and decreases it. You check for full before pushing and empty before popping.
What is a menu-driven stack program?
A menu-driven stack program shows the user a list of choices — push, pop, peek, display, exit — and repeats in a loop, performing whichever operation the user selects. It is a common way to practise data structures because it exercises every operation interactively.
What is the peek operation in a stack?
peek (sometimes called top) returns the value of the item currently on top of the stack without removing it. It lets you look at the next item to be popped while leaving the stack unchanged.
How do you display all elements of an array stack?
You loop from the current top index down to 0, printing each element. Going from top down shows the items in the order they would be popped, which reflects the LIFO nature of the stack.
What is the advantage of an array stack over a linked-list stack?
An array stack is simpler and uses less memory per element because it does not store pointers, and access is very fast. Its main limit is a fixed maximum size decided in advance, whereas a linked-list stack can grow freely at run time.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.