Friday, March 13, 2020

Program of Stack using Array in C++

// Stack With array

#include <iostream>
using namespace std;

int stack[100];

int top = -1;

void push(int x)
{
if (top == 99)
{
cout<<"\nOverflow";
}
else
{
stack[++top] = x;
}
}

void pop()
{
if (top == -1)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<stack[top]<<" deleted";
top--;
}
}

void show()
{
if (top == -1)
{
cout<<"\n Stack is empty";
}
else
{
cout<<"\nTopmost element: " <<stack[top];
}
}

int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print top";
cout<<"\n4. Exit";
cout<<"\nEnter Your Choice : ";
cin>> ch;
if (ch == 1)
{
cout<<"\nEnter element : ";
cin>>x;
push(x);
}
else if (ch == 2)
{
pop();
}
else if (ch == 3)
{
show();
}

} while (ch != 4);

return 0;
}



No comments:

Post a Comment