Vous êtes sur la page 1sur 3

Write a C++ Program for Push & Pop.

The Program should ask the user to select the choice Push
or Pop, and should return the result according to user choice. The Program should be run as
much time as user want to run.

#include <iostream>
using namespace std;
int stack[10], n=10, top=-1;
void push(int value)
{
if(top>=n-1)
cout<<"Stack is Full OR Stack is Overflow"<<endl;
else
{
top++;
stack[top]=value;
}
}

void pop()
{
if(top<=-1)
cout<<"Stack is Empty OR Stack is 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]<<endl;
}
else
cout<<"Stack is Empty: ";
}

int main()
{
int a, Stack();
char ch;
do{
int a;
cout<<"Select One in the Following Choices\n \n1.Push Item \n2.Pop Item
\n3.Display Item \n4.Exit Stack\n";
cout<<"\nPlease enter your choice: ";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter an Integer Number: ";
cin>>a;
push(a);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
cout<<"Exit"<<endl;
break;

default :
cout<<"An Invalid Choice!!!";

}
cout<<"\nDo you want to continue? ";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}

Vous aimerez peut-être aussi