C++ Program to Implement a Stack Using Arrays

A stack is a fundamental data structure that operates on a Last In First Out (LIFO) principle. In this structure, elements are added (pushed) and removed (popped) from the top of the stack. This article will guide you through the implementation of a stack using arrays in C++, providing multiple examples to demonstrate various operations and their outputs.

Prerequisites

Before diving into the implementation, you should have:

  1. Basic understanding of C++ programming.
  2. Familiarity with arrays.
  3. Knowledge of stack operations.

With these prerequisites, you will be able to grasp the concepts and code implementations discussed here.

1. Stack Implementation Using Arrays

1.1 Stack Structure and Initialization

Let’s start by defining the structure of a stack and initializing it.

C++
#include <iostream>
#define MAX_SIZE 5

class Stack {
public:
    Stack() : top(-1) {}

    void push(int data) {
        if (top == MAX_SIZE - 1) {
            std::cout << "Stack is full" << std::endl;
            return;
        }
        stack[++top] = data;
    }

    void pop() {
        if (top == -1) {
            std::cout << "Stack is empty" << std::endl;
            return;
        }
        std::cout << "Popped: " << stack[top--] << std::endl;
    }

    void display() {
        if (top == -1) {
            std::cout << "Stack is empty" << std::endl;
            return;
        }
        std::cout << "Stack: ";
        for (int i = top; i >= 0; i--) {
            std::cout << stack[i] << " ";
        }
        std::cout << std::endl;
    }

private:
    int stack[MAX_SIZE];
    int top;
};

int main() {
    Stack stack;
    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.push(40);
    stack.push(50);

    std::cout << "Initial Stack: ";
    stack.display();

    return 0;
}

1.2 Output Explanation

In this example, the stack is structured as follows:

C++
Stack: 50 40 30 20 10

The output will display: Initial Stack: 50 40 30 20 10

2. Advanced Stack Operations

2.1 Push and Pop Operations

Let’s demonstrate the push and pop operations with more details.

C++
int main() {
    Stack stack;
    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.push(40);
    stack.push(50);

    std::cout << "Stack after pushing 10, 20, 30, 40, 50: ";
    stack.display();

    stack.pop();
    stack.pop();
    std::cout << "Stack after popping two elements: ";
    stack.display();

    stack.push(60);  // Adding more elements
    stack.display();

    return 0;
}

2.2 Output Explanation

In this example, the stack undergoes the following transformations:

  • After pushing 10, 20, 30, 40, 50:Stack: 50 40 30 20 10 The output will display: Stack after pushing 10, 20, 30, 40, 50: 50 40 30 20 10
  • After popping two elements:Popped: 50 Popped: 40 Stack: 30 20 10 The output will display: Stack after popping two elements: 30 20 10
  • After pushing 60:Stack: 60 30 20 10 The output will display: Stack: 60 30 20 10

3. Handling Edge Cases

3.1 Full and Empty Stack

Let’s handle the edge cases where the stack is full or empty.

C++
int main() {
    Stack stack;

    stack.pop();  // Attempting to pop from an empty stack

    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.push(40);
    stack.push(50);
    stack.push(60);  // Attempting to push in a full stack

    stack.display();

    return 0;
}

3.2 Output Explanation

In this example, attempting to pop from an empty stack and push in a full stack will produce the following output:

C++
Stack is empty
Stack is full
Stack: 50 40 30 20 10

This ensures that the program handles edge cases gracefully without crashing.

Conclusion

A C++ program to implement a stack using arrays involves defining the stack structure and implementing various operations such as push, pop, and display. This article provided a detailed implementation of these operations along with multiple examples to illustrate their functionality