Show Step by step working of a stack of size 5? Push (8) Pop ( ) Display ( ) Push (5) Push (28) Pop ( ) Display ( ) Push (55) Pop ( ) Pop ( ) Display ( )
Show Step by step working of a stack
of size 5? 
Push
(8) 
Pop
( ) 
Display
( ) 
Push
(5) 
Push
(28) 
Pop
( ) 
Display
( ) 
Push
(55) 
Pop
( ) 
Pop
( ) 
Display
( ) 
Program:
#include<iostream>
using namespace std;
int stack[5],n=5,top=-1,val;
void push(int);
void pop();
void display();
int
main()
{
            
            push(8);
            pop();
            display();
            push(5);
            push(28);
            pop();
            display();
            push(55);
            pop();
            pop();
            display();
}
void
push(int val)
{
            if
(top>=n-1)
            {
                        cout<<"Stack
overflow"<<endl;
            }
            else
            {
                        top++;
                        stack[top]=val;
            }
}
void
pop()
{
            if(top<=-1)
            {
            cout<<"Stack
underflow"<<endl;
            }
            else 
            {
                        cout<<"The
popped element is"<<stack[top]<<endl;
                        top--;
            }
}
void
display()
{
            if(top>=0)
            {
                        cout<<"Stack
elements are:";
                        for(int
i=top;i>=0;i--)
                        {
                                    cout<<stack[i]<<"
";
                                    cout<<endl;
                        }
            }
}


Comments
Post a Comment