C++ - STACK Implementation using Array with PUSH, POP, TRAVERSE Operations

Below I have written C++ Program to Implement Stack using array, the basic operations of stack are PUSH(), POP() and DISPLAY(). PUSH function is used to insert an element to the top of stack, POP function used to delete or remove an element from the top of stack and at last DISPLAY function used to show the values. In this, I share C++ Program to Implement Stack 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 Above article, I shared C++ Program to Implement Stack using array or we can say Implement stack using static memory allocation. If you have any problem in above article then comment your problem.
Happy Coding 😍

Share this

0 Comment to "C++ - STACK Implementation using Array with PUSH, POP, TRAVERSE Operations"

Post a Comment