Representation of a Stack using Array | C++ Solution

Stack using an array in C++

 

Stack is one of the most important linear data structures, that we can use in different algorithms or implement the application in various problem solving. A stack is a linear data structure, that means it can be easily implememented using an array. You can use array to store some elements in the specific order you recieve them. Then you can use simple easy techniques to manage the data so that it can work like an Stack. Here we will go through the Representation of a Stack using Array.

Representation using an Array

There are some major differences between a stack and an array. like,

  • Array size is fixed, but the same isn’t applied for stack.
  • Also, in a stack you can only pop or push values only from or in the last element

Now let us focus on how we can implement a stack using an array.

For Example:

We are given a stack of elements:  2, 4, 3, 1, 5.

Step 1:

  • Push (5).
  •  Top of stack = 5
  • Element is inserted at a[4].

Step 2:

  • Push (1).
  •  Top of stack = 1
  • Element is inserted at a[3].

Step 3:

  • Push (4).
  •  Top of stack = 4
  • Element is inserted at a[2].
Stack using an array

Step 4:

  • Push (3).
  •  Top of stack = 3
  • Element is inserted at a[1].
Stack using an array

Step 5:

  • Push (2).
  •  Top of stack = 2
  • Element is inserted at a[0].
Stack using an array step 6

Code for Stack in C++ (using Class)

// Cpp Program for Implmentation of stack (array) using structure
#include<bits/stdc++.h>
using namespace std;

class Stack
{
    public:
    int top;
    int maxSize;
    int* array;
    Stack(int max)
    {
        top=-1;
        maxSize=max;
        array=new int[max];
    }
    int isFull()
    {
        if(top==maxSize-1)
        cout<<"Will not be able to push maxSize reached"<<endl;
        return top==maxSize-1;
    }

    int isEmpty()
    {
        if(top==-1)
        cout<<"Will not be able to pop minSize reached"<<endl;
        return top==-1;
    }
    void push(int item)
    {
        if(isFull()) return;
        array[++top]=item;
        cout<<"We have pushed "<<item<<" to stack"<<endl;
    }
    int pop()
    {
        if(isEmpty()) return INT_MIN;
        return array[top--];
    }
    int peek()
    {
        if(isEmpty()) return INT_MIN;
        return array[top];
    }
};

int main()
{
    Stack stack(10);
    stack.push(5);
    stack.push(10);
    stack.push(15);
    int flag=1;
    while(flag)
    {
        if(!stack.isEmpty())
            cout<<"We have popped "<< stack.pop()<<" from stack"<<endl;
        else
            cout<<"Can't Pop stack must be empty\n";
            
           cout<<"Do you want to Pop again? Yes: 1 No: 0\n";
           cin>>flag;
    }

}

O/P

We have pushed 5 to stack                         
We have pushed 10 to stack
We have pushed 15 to stack
We have popped 15 from the stack
Do you want to Pop again? Yes: 1 No: 0

Implementation of a stack using an Array (Without Class)

#include<bits/stdc++.h>
using namespace std;
#define size 20

int Stack[size],top_of_stack;

void Push(int [],int); //to insert elements in the stack
void Pop (int []);//to delete elements from the stack
void display(int []); //to display the elements


int main()
{
    int num=0;
    int option=0;
    top_of_stack=-1;


    while(1)
    {
        cout<<"\n 1: Push \n 2: Pop \n 3: Display \n 4: Exit \n Enter one of your choices: ";
        cin>>option;


        switch(option)
        {
            case 1:
                cout<<"Enter the item you want to insert :";
                cin>>num;
                Push(Stack,num);
            break;


            case 2:
                Pop(Stack);
            break;


            case 3:
                display(Stack);
            break;


            case 4:
                exit(0);


            default:
                cout<<"\nPlease enter a correct option\n";
            break;
        }
    }


}


void Push(int stack[],int num)
{
    if(top_of_stack==size-1)
    {
        cout<<"\nCannot add element, Stack is full\n";
        return;
    }
    top_of_stack++;
    stack[top_of_stack]=num;
}


void Pop(int stack[])
{
    int del_num;
    if(top_of_stack==-1)
    {
        cout<<"Empty Stack.\n";
        return;
    }


    del_num=stack[top_of_stack];
    top_of_stack--;
    cout<<"Element deleted: "<<del_num<<endl;
    return;
}


void display(int stack[])
{
    int i=0;
    if(top_of_stack==-1)
    {
        cout<<"Empty Stack .\n";
        return;
    }


    cout<<"Top of stack is: "<<stack[top_of_stack]<<endl;
    for(i = top_of_stack ; i >=0 ; i--)
    {
        cout<<" "<<stack[i]<<" ";
    }
    cout<<"\n\n";
} 

O/P

1: Push
2: Pop
3: Display
4: Exit
Enter one of your choices: 1 Enter the item you want to insert :1 1: Push 2: Pop 3: Display 4: Exit Enter one of your choices: 1 Enter the item you want to insert :2 1: Push 2: Pop 3: Display 4: Exit Enter one of your choices: 1 Enter the item you want to insert :3 1: Push 2: Pop 3: Display 4: Exit Enter one of your choices: 2 Element deleted: 3 1: Push 2: Pop 3: Display 4: Exit Enter one of your choices: 3 Top of stack is: 2 1 1: Push 2: Pop 3: Display 4: Exit Enter one of your choices: 4

What is Stack?

Stack is a data structure where you can add any kind of and store then in a LIFO (last in first out) manner. To kno more about Stack in C++ STL :

Stack in C++ STL

suppose you push 2,3,4 in to the stack respectively. When you pop out a value, the last pushed value (4) will be deleted from it. Then if you try to see what is in the top of the stack, you will find its the second last value entered (2)

Article is contributed by Rahit Saha