×

DS - Basics

DS - Array

DS - Linked List

DS - Stack

DS - Queue

DS Hashing

DS Tree

DS - Graph

DS Programs Using C/C++

DS - Miscellaneous Topics

Implement of stack using array

Last Updated : December 02, 2025

A stack is a fundamental linear data structure that stores elements in a practical and organized way using the Last In, First Out (LIFO) principle. In a stack, elements are inserted and removed from the top only.

This article explains how to implement a stack using arrays in C, perform operations like push, pop, and display, and discusses its advantages and disadvantages.

stack implementation using Array in C

Stack Operations

Stack operations include inserting an element (push), removing an element (pop), and displaying all elements (display). These operations are explained below:

Push Operation

The push operation adds an element to the top of the stack. Before insertion, it checks whether the stack is full. If full, it does not insert the element; otherwise, it adds the element at the top.

Pop Operation

The pop operation removes the topmost element of the stack. It first checks if the stack is empty. If not empty, it deletes the top element and displays it.

Display Operation

The display function prints all the elements in the stack. If the stack is empty, it outputs “Stack is Empty”.

Note: The stack in this program can hold up to 5 items. You can change the capacity by modifying the array size in the code.

Algorithm to Implement Stack Using Array

Algorithm Steps

  1. Initialize an array stack[MAX] and a variable top = -1 to track the top element.
  2. To Push (insert) an element:
    1. Check if the stack is full: top == MAX - 1. If yes, display "Stack Overflow".
    2. If not full, increment top by 1.
    3. Insert the element at stack[top].
  3. To Pop (delete) an element:
    1. Check if the stack is empty: top == -1. If yes, display "Stack Underflow".
    2. If not empty, print the element at stack[top].
    3. Decrement top by 1 to remove the element.
  4. To Display elements of the stack:
    1. If top == -1, print "Stack is empty".
    2. Else, iterate from 0 to top and print each stack[i].

Pseudocode for Stack Using Array

Initialize array stack[MAX]
Initialize top = -1

Function PUSH(value):
    If top == MAX - 1:
        Print "Stack Overflow"
    Else:
        top = top + 1
        stack[top] = value

Function POP():
    If top == -1:
        Print "Stack Underflow"
    Else:
        Print "Popped element: " + stack[top]
        top = top - 1

Function DISPLAY():
    If top == -1:
        Print "Stack is empty"
    Else:
        For i = 0 to top:
            Print stack[i]

Main Function:
    Do:
        Print "1. Push  2. Pop  3. Display  4. Exit"
        Read choice
        Switch choice:
            Case 1: Read value, call PUSH(value)
            Case 2: Call POP()
            Case 3: Call DISPLAY()
            Case 4: Exit
            Default: Print "Invalid choice"
    While choice != 4

C Program to Implement Stack Using Array

#include <stdio.h>
#define MAX 5

int stack[MAX];
int top = -1;

// Function to insert element in stack
void push() {
    int value;
    if(top == MAX - 1)
        printf("Stack is full\n");
    else {
        printf("Enter a value to push: ");
        scanf("%d", &value);
        stack[++top] = value;
    }
}

// Function to delete element from stack
void pop() {
    if(top == -1)
        printf("Stack is empty\n");
    else
        printf("Popped element: %d\n", stack[top--]);
}

// Function to display stack elements
void display() {
    if(top == -1)
        printf("Stack is empty\n");
    else {
        printf("Stack elements: ");
        for(int i = 0; i <= top; i++)
            printf("%d ", stack[i]);
        printf("\n");
    }
}

int main() {
    int choice;
    do {
        printf("\n1.Push  2.Pop  3.Display  4.Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice) {
            case 1: push(); break;
            case 2: pop(); break;
            case 3: display(); break;
            case 4: printf("Exiting..."); break;
            default: printf("Invalid choice\n");
        }
    } while(choice != 4);
    return 0;
}

Advantages of Stack

  • Simple and easy to implement.
  • Efficient memory usage since operations occur at one end.
  • Supports recursive programming effectively.
  • Useful in expression evaluation, backtracking, and function call management.

Disadvantages of Stack

  • Limited access: only the top element can be accessed directly.
  • Fixed size in array implementation; dynamic resizing requires extra coding.
  • Cannot search or modify elements except the top one easily.

Read More

For detailed algorithms and examples, read more about stack implementation in C and C++.

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.