Operations of Stack in C++ using Array

Today we are going to explain the operations of Stack in C++ using Array. Operations of Stacks like Push and pop operation Also the Limitations of Arrays and its benefits.

Stack in C++

A stack in C++ is a fundamental data structure in computer science that follows the Last In, First Out (LIFO) principle. It operates much like a stack of plates or books where you can only add or remove items from the top.

In a stack, elements are added and removed from the same end, known as the “top” of the stack. This means that the most recently added element is the first one to be removed.

Operations of stack in C++

The key operations of a stack are:

  • Push Operation
  • pop operation
  • Top Element
  • Empty(or IsEmpty)
operations of Stack in C++ using Array

push operation

The push operation is used to add an element onto the top of the stack. When a new element is pushed onto the stack, it becomes the new top element, and the size of the stack increases by one. This operation is also known as insertion.

In terms of implementation, when using an array, pushing an element involves appending it to the end of the array or incrementing a pointer to the top of the stack and then placing the element at that position.

 void push(int x)
    {
        //if top is on last index
        if (top == n - 1) {
            cout << "stack is over flow" << endl;
            return;
        }
        top++;
        arr[top] = x;
    }

pop operation

The pop operation is used to remove the top element from the stack. After popping an element, the size of the stack decreases by one, and the element that was removed is returned or discarded, depending on the implementation. This operation is also known as deletion. In array-based implementations, popping involves removing the element at the top of the stack by decrementing the top pointer or index.

void pop()
    {
        if (top == NULL) {
            cout << "no element to pop" << endl;
            return;
        }
        top--;
    }

Top (or Peek)

The top operation, also known as peek, is used to retrieve the top element of the stack without removing it. It allows you to access the element that is currently at the top of the stack without modifying the stack’s contents. This operation is useful for inspecting the top element or performing operations based on its value.

 int topElement()
    {
        if (top == NULL) {
            cout << "no element in stack" << endl;
            return -1;
        }

        else
            return arr[top];
    }

Empty (or IsEmpty)

The empty operation, sometimes called isEmpty, is used to check if the stack is empty or not. It returns a boolean value indicating whether the stack contains any elements or if it is devoid of elements. This operation is crucial for avoiding errors like attempting to pop from an empty stack.

 bool empty()
    {
        if (top == NULL) {

            return 1;
        }
        else

            return 0;
    }

Benefits of Array-Based Stack Implementation

Implementing a stack using arrays offers several advantages:

  1. Simplicity: The implementation is straightforward, making it easy to understand and maintain.
  2. Efficiency: Both push and pop operations have a time complexity of O(1), providing efficient performance for basic stack operations.
  3. Memory Efficiency: Arrays allocate contiguous memory, ensuring memory efficiency compared to other data structures that might require dynamic memory allocation.
  4. Flexibility: Arrays can be resized dynamically, allowing the stack to grow or shrink as needed.

Limitations

While array-based stack implementations offer many advantages, it’s essential to consider potential limitations, such as:

  1. Fixed Size: Traditional arrays have a fixed size, which can limit the capacity of the stack. However, this can be mitigated by using dynamic arrays or by implementing resizing mechanisms.
  2. Wasted Space: If the stack’s capacity is greater than the number of elements it contains, there may be wasted space in the array. Again, dynamic resizing can help optimize memory usage.

NOTE

Get the Project Files from GitHub.

You can also download its Zip File by clicking on Download button.

Conclusion

Implementing an operations of Stack in C++ using Array provides a simple yet powerful solution for managing data in a Last In, First Out fashion. With efficient push and pop operations, along with the flexibility and memory efficiency of arrays, this approach offers an ideal balance of performance and simplicity.

Whether you’re tackling algorithmic problems, building software applications, or exploring computer science concepts, understanding and mastering stack implementation using arrays is a valuable skill that will serve you well in your programming journey. So, embrace the power of arrays and unlock the full potential of stack-based solutions in your projects.

Code of operations of Stack in C++ using Array

This is the complete code of stack and its operations in C++ using Array.

// stackBasic.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
using namespace std;
#define n 100


class stack
{
    int* arr;
    int top;
public:

    stack()
    {
        arr = new int[n];
        top = NULL;
    }
    void push(int x)
    {
        //if top is on last index
        if (top == n - 1) {
            cout << "stack is over flow" << endl;
            return;
        }
        top++;
        arr[top] = x;
    }
    void pop()
    {
        if (top == NULL) {
            cout << "no element to pop" << endl;
            return;
        }
        top--;
    }
    int topElement()
    {
        if (top == NULL) {
            cout << "no element in stack" << endl;
            return -1;
        }

        else
            return arr[top];
    }

    bool empty()
    {
        if (top == NULL) {

            return 1;
        }
        else

            return 0;
    }
};




int main()
{
    stack stack;
    //cout << "Pushing elements onto stack" << endl;

    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);


    cout << "current top element after pushes: " << endl;
    cout << stack.topElement() << endl;
    cout << "popping top element: " << endl;
    stack.pop();

    cout << stack.topElement() << endl;
    cout << "popping top element: " << endl;
    stack.pop();
    cout << stack.topElement() << endl;
    cout << "popping top element: " << endl;
    stack.pop();
    cout << stack.topElement() << endl;

    stack.pop();
    cout << "stack is now empty." << endl;
    cout << stack.empty() << endl;

    // return;
}

Leave a comment