16. DSA with C++ - Stack using dynamic arrays

Stack using dynamic arrays in C++

This is an implementation of a stack using a dynamic array in C++, along with its basic operations: push, pop, peek/top, isEmpty, and size.


#include <iostream>

class Stack {
private:
    int* arr;       // Dynamic array to store stack elements
    int capacity;   // Maximum capacity of the stack
    int top;        // Index of the top element (-1 if stack is empty)

public:
    // Constructor to initialize an empty stack with a given capacity
    Stack(int capacity) {
        arr = new int[capacity];
        this->capacity = capacity;
        top = -1;
    }

    // Destructor to free the dynamically allocated memory
    ~Stack() {
        delete[] arr;
    }

    // Push an element onto the top of the stack
    void push(int value) {
        if (top == capacity - 1) {
            std::cout << "Stack overflow! Cannot push element " << value << std::endl;
            return;
        }
        arr[++top] = value;
        std::cout << "Pushed element " << value << " onto the stack." << std::endl;
    }

    // Pop the top element from the stack and return its value
    int pop() {
        if (isEmpty()) {
            std::cout << "Stack underflow! Cannot pop element from an empty stack." << std::endl;
            return -1;  // Return a default value
        }
        int value = arr[top--];
        std::cout << "Popped element " << value << " from the stack." << std::endl;
        return value;
    }

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

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

    // Return the current size of the stack
    int size() {
        return top + 1;
    }
};

int main() {
    // Create a stack with capacity 5
    Stack stack(5);

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

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

    // Pop elements from the stack
    std::cout << "Popped element: " << stack.pop() << std::endl;
    std::cout << "Popped element: " << stack.pop() << std::endl;

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

    // Get the current size of the stack
    std::cout << "Stack size: " << stack.size() << std::endl;

    return 0;
}
    

In this example, we create a Stack class that uses a dynamic array to implement the stack data structure. Here's a breakdown of the operations:

  1. push(int value): Inserts an element onto the top of the stack. If the stack is already full, it displays an overflow message.
  2. pop(): Removes and returns the top element from the stack. If the stack is empty, it displays an underflow message.
  3. peek(): Returns the value of the top element without removing it from the stack. If the stack is empty, it displays an appropriate message.
  4. isEmpty(): Checks if the stack is empty and returns true if it is, or false otherwise.
  5. size(): Returns the current size of the stack.

In the main() function, we create a Stack object with a capacity of 5. We then demonstrate the usage of various stack operations, including pushing elements onto the stack, popping elements from the stack, getting the top element, checking if the stack is empty, and obtaining the size of the stack.

When you run the code, the output will be:


Pushed element 10 onto the stack.
Pushed element 20 onto the stack.
Pushed element 30 onto the stack.
Top element: 30
Popped element: 30
Popped element: 20
Is stack empty? No
Stack size: 1
    

This example demonstrates the implementation of a stack using a dynamic array in C++, along with its basic operations.