DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Stack (Array Implementation), Code ↔ Language

Creation of Stack (Array Implementation)

class Stack {
private:
    int* arr;
    int top;
    int capacity;

public:
    // Constructor to initialize stack
    Stack(int size) {
        arr = new int[size];
        capacity = size;
        top = -1;
    }
};
Enter fullscreen mode Exit fullscreen mode
  1. First, we will create a class named Stack.

  2. It will contain three properties:

    • int* arr: Points to the array that will store stack elements.
    • int top: Stores the index of the top element in the stack.
    • int capacity: Stores the maximum number of elements the stack can hold.
  3. We will declare a constructor Stack(int size):

    • It will take size as input.
    • We will dynamically allocate memory for the stack array with the given size and assign it to arr.
    • We will set the capacity to the given size.
    • We will set top to -1, indicating an empty stack.

Push Operation

void push(int item) {
    if (isFull()) {
        cout << "Overflow: Stack is full.\n";
        return;
    }
    arr[++top] = item;
}
Enter fullscreen mode Exit fullscreen mode

We will define a method push(int item):

  • It will take an integer item as input, which is the value to be pushed onto the stack.
  • We will check if the stack is full (using isFull()); if so, it will print "Overflow: Stack is full." and return.
  • Otherwise, it will increment the top index and assign the value item to the stack at the updated top index.

Pop Operation

int pop() {
    if (isEmpty()) {
        cout << "Underflow: Stack is empty.\n";
        return -1;
    }
    return arr[top--];
}
Enter fullscreen mode Exit fullscreen mode

We will define a method pop():

  • It will return an integer, which is the value removed from the top of the stack.
  • We will check if the stack is empty (using isEmpty()); if so, it will print "Underflow: Stack is empty." and return -1.
  • Otherwise, it will return the value at the top index and then decrement the top index.

Peek Operation

int peek() {
    if (isEmpty()) {
        cout << "Stack is empty.\n";
        return -1;
    }
    return arr[top];
}
Enter fullscreen mode Exit fullscreen mode

We will define a method peek():

  • It will return an integer, which is the value at the top of the stack.
  • We will check if the stack is empty (using isEmpty()); if so, it will print "Stack is empty." and return -1.
  • Otherwise, it will return the value at the top index.

isEmpty Operation

bool isEmpty() {
    return top == -1;
}
Enter fullscreen mode Exit fullscreen mode

We will define a method isEmpty():

  • It will return a boolean value indicating whether the stack is empty.
  • It will return true if top is -1, indicating that the stack is empty.
  • Otherwise, it will return false.

isFull Operation

bool isFull() {
    return top == capacity - 1;
}
Enter fullscreen mode Exit fullscreen mode

We will define a method isFull():

  • It will return a boolean value indicating whether the stack is full.
  • It will return true if top is equal to capacity - 1, indicating that the stack has reached its maximum capacity.
  • Otherwise, it will return false.

Size Operation

int size() {
    return top + 1;
}
Enter fullscreen mode Exit fullscreen mode

We will define a method size():

  • It will return an integer representing the number of elements currently in the stack.
  • It will return top + 1, which gives the count of elements in the stack.

Full Code Implementation

#include <iostream>
using namespace std;

class Stack {
private:
    int* arr;
    int top;
    int capacity;

public:
    // Constructor to initialize stack
    Stack(int size) {
        arr = new int[size];
        capacity = size;
        top = -1;
    }

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

    // Push operation
    void push(int item) {
        if (isFull()) {
            cout << "Overflow: Stack is full.\n";
            return;
        }
        arr[++top] = item;
    }

    // Pop operation
    int pop() {
        if (isEmpty()) {
            cout << "Underflow: Stack is empty.\n";
            return -1;
        }
        return arr[top--];
    }

    // Peek operation
    int peek() {
        if (isEmpty()) {
            cout << "Stack is empty.\n";
            return -1;
        }
        return arr[top];
    }

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

    // Check if stack is full
    bool isFull() {
        return top == capacity - 1;
    }

    // Get the size of the stack
    int size() {
        return top + 1;
    }
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)