Exp 4
Exp 4
Roll no-2059 S7
#include<iostream>
using namespace std;
class stack
{
int stackarray[10];
int top;
int maxsize;
public:
stack() //non paramitrized constructor
{
top=-1;
maxsize=5;
}
void push(int x)
{
if(top==maxsize-1)
{
cout<<"stack overflow"<<endl;
}
else
{
top=top+1;
stackarray[top]= x;
}}
void pop()
{
if(top==-1)
{
cout<<"stack is empty"<<endl;
}
else
{
cout<<"poped element"<<stackarray[top]<<endl;
top--;
}}
void display()
{
if(top==-1)
{
cout<<"stack underflow"<<endl;
}
else
{
for(int i=top;i>-1;i--)
{
cout<<stackarray[i]<<endl;
}
}
}
~stack() //destructor
{
cout<<"destructor called"<<endl;
}
};
int main()
{
stack s;
int num;
int choice;
do{
cout<<"enter the operation"<<endl;
cout<<"1.push"<<endl<<"2.pop"<<endl<<"3.display"<<endl<<"4.exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter the num"<<endl;
cin>> num;
s.push(num);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
cout<<"exit"<<endl;
}}
while(choice!=4);
{return 0;
}
return 0;
}