15. DSA with C++ - Stack using Array

Stack using Array

This is an implementaon of a stack using an array in C++. It includes all the common operaons of a stack, such as push, pop, peek, and checking if the stack is empty or full.


#include <iostream>
#define MAX_SIZE 100

class Stack {
private:
    int arr[MAX_SIZE];
    int top;

public:
    // Constructor
    Stack() {
        top = -1;
    }

    // Function to push an element onto the stack
    void push(int value) {
        if (top == MAX_SIZE - 1) {
            std::cout << "Stack Overflow: Cannot push element. Stack is full." << std::endl;
            return;
        }
        arr[++top] = value;
    }

    // Function to pop an element from the stack
    void pop() {
        if (isEmpty()) {
            std::cout << "Stack Underflow: Cannot pop element. Stack is empty." << std::endl;
            return;
        }
        --top;
    }

    // Function to retrieve the top element from the stack without removing it
    int peek() {
        if (isEmpty()) {
            std::cout << "Stack is empty." << std::endl;
            return -1;
        }
        return arr[top];
    }

    // Function to check if the stack is empty
    bool isEmpty() {
        return top == -1;
    }

    // Function to check if the stack is full
    bool isFull() {
        return top == MAX_SIZE - 1;
    }
};

int main() {
    Stack stack;

    // Push elements onto the stack
    stack.push(10);
    stack.push(20);
    stack.push(30);

    // Print the top element
    std::cout << "Top element: " << stack.peek() << std::endl;

    // Pop an element from the stack
    stack.pop();

    // Print the top element
    std::cout << "Top element: " << stack.peek() << std::endl;

    // Check if the stack is empty
    std::cout << "Is stack empty? " << (stack.isEmpty() ? "Yes" : "No") << std::endl;

    return 0;
}
        

In this code, we define a Stack class that has an array arr to store the elements and a variable top to keep track of the top element's index.

Here's a breakdown of the operaons:
  1. Constructor: Ini alizes the top variable to -1.
  2. push funcon: Adds an element to the top of the stack. It first checks if the stack is already full (top == MAX_SIZE - 1), and if so, displays an error message. Otherwise, it increments top and assigns the value to the corresponding index in the array.
  3. pop funcon: Removes the top element from the stack. It first checks if the stack is empty (top == -1), and if so, displays an error message. Otherwise, it decrements top.
  4. peek funcon: Retrieves the top element from the stack without removing it. It checks if the stack is empty and, if not, returns the element at the top index.
  5. isEmpty funcon: Checks if the stack is empty by comparing top with -1. It returns true if the stack is empty and false otherwise.
  6. isFull funcon: Checks if the stack is full by comparing top with MAX_SIZE - 1. It returns true if the stack is full and false otherwise.
  7. In the main funcon, we

    create a Stack object and perform various stack operaons:

    - Push three elements onto the stack.

    - Print the top element using the peek function.

    - Pop an element from the stack.

    - Print the new top element.

    - Check if the stack is empty
Output

Top element: 30 
Top element: 20 
Is stack empty? No