Adeela Shaheen 2125165002
Adeela Shaheen 2125165002
Adeela Shaheen 2125165002
}
void display(){
cout<<"values in stack = "<<endl;
for(int i=top; i>=0; i--)
cout<<stk[i]<<endl;
}
};
main(){
PushPop p;
int n;
cout<<"press 1 for push 2 for pop 3 for display "<<endl;
do
{
cin>>n;
switch (n)
{
case 1:
p.push();
break;
case 2:
p.pop();
break;
case 3:
p.display();
break;
default:
cout<<"invalid input";
}
}
while(n>=1);
}
Push and Pop functions in Stack and display even numbers
#include<iostream>
using namespace std;
class test
{
public:
int stk[10];
int top=-1;
void push()
{
if(top==9)
{
cout<<"over flow "<<endl;
}
else
{
for(int i=0;i<10;i++)
{
top++;
cout<<"enter value in stack = ";
cin>>stk[top];
}
}
}
void pop()
{
if(top<0)
{
cout<<"under flow ";
}
else
{
stk[top]=0;
top--;
}
}
void display()
{
if(top==-1)
cout<<"stack is empty :";
else
{
cout<<"even values in stack ="<<endl;
for(int i=top;i>=0;i--)
{
if(stk[i]%2==0)
cout<<stk[i]<<endl;
}
}
}
};
int main()
{
test t;
t.push();
t.pop();
t.display();
return 0;
}