Program in c++ to perform stack operations using array

In this tutorial, I shared program in c++ to perform stack operations using array. Implementation of stack c++ program example.

push and pop operation in stack in data structure


push function is used to insert an element to the top of the stack, pop function used to delete or remove an element from the top of the stack and at last display function used to show the values. In this, I share the c++ program to implement stack using the array.

push and pop operation in stack in c++ program using array


using namespace std;
#include<iostream> //iostream as header
#define size 100

int stack[size],choice,n,top,item,i; //gobal variables declare

void push(); //functions declare
void pop();
void display();

int main()
{
    top=-1;
    cout<<"\n Enter the size of STACK[MAX=100]:";
    cin>>n;
    cout<<"\n\t STACK OPERATIONS USING ARRAY";
    cout<<"\n\t--------------------------------";
    cout<<"\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT";
    while(1)
    {
        cout<<"\n Enter the Choice:";
        cin>>choice;
        switch(choice)
        {
            case 1:
            {
                push();
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                display();
                break;
            }
            case 4:
            {
                exit(0);
                break;
            }
            default:
            {
                printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
            }
                 
        }
    }
    return 0;
}
void push() //function definition 
{
    if(top>=n-1)
    {
        cout<<"\n\tSTACK is Overflow";
    }
    else
    {
        cout<<" Enter a value to be Pushed:";
        cin>>item;
        top++;
        stack[top]=item;
    }
}
void pop()
{
    if(top<=-1)
    {
        cout<<"\n\t Stack is Underflow";
    }
    else
    {
        cout<<"\n\t The Popped elements is"<=0)
    {
        cout<<"\n The Elements in STACK \n";
        for(i=top; i>=0; i--)
            cout<<"\n Stack[i]";
        cout<<"\n Press Next Choice";
    }
    else
    {
        cout<<"\n The STACK is empty";
    }
    
}

OUTPUT:




 Enter the size of STACK[MAX=100]:5


         STACK OPERATIONS USING ARRAY
        --------------------------------
         1.PUSH
         2.POP
         3.DISPLAY
         4.EXIT
 Enter the Choice:3

 The STACK is empty
 Enter the Choice:2

         Stack is Underflow
 Enter the Choice:1
 Enter a value to be Pushed:10

 Enter the Choice:1
 Enter a value to be Pushed:20

 Enter the Choice:1
 Enter a value to be Pushed:30

 Enter the Choice:1
 Enter a value to be Pushed:40

 Enter the Choice:1
 Enter a value to be Pushed:50

 Enter the Choice:1

        STACK is Overflow
 Enter the Choice:3

 The Elements in STACK
50
40
30
20
10

 Press Next Choice
 Enter the Choice:2

         The Popped elements is50
 Enter the Choice:2

         The Popped elements is40
 Enter the Choice:3

 The Elements in STACK
30
20
10

 Press Next Choice
 Enter the Choice:2

         The Popped elements is30
 Enter the Choice:2

         The Popped elements is20
 Enter the Choice:2

         The Popped elements is10
 Enter the Choice:2

         Stack is Underflow
 Enter the Choice:3

 The STACK is empty
 Enter the Choice:4


In the above article, I shared Program in c++ to perform stack operations using array. If you have any problem in the above article then comment on your problem.
Happy Coding 😍

If you have any doubts, Please let me know

Previous Post Next Post

Contact Form