Program:-
#include <iostream>
using namespace std;
class stack{
int top;
int arr[5];
public:
stack(){
top=-1;
for(int i=0; i<5; i++){
arr[i]=0;
}
}
bool isempty(){
if(top==-1)return true;
return false;
}
bool isfull(){
if(top==4)return true;
return false;
}
void push(int d){
if(isfull()){
cout<<"The stack is overflow"<<endl;
}
else{
top++;
arr[top]=d;
}
}
int pop(){
if(isempty()){
cout<<"stack is underflow"<<endl;
return 0;
}
else{
int val=arr[top];
arr[top]=0;
top--;
return val;
}
}
void display(){
cout<<"The stack is\n "<<endl;
for(int i=4; i>=0; i--){
cout<<" | "<<arr[i]<<" |"<<endl;
cout<<" _____"<<endl;
}
cout<<endl;
}
};
int main()
{
stack s;
s.push(2);
s.push(14);
s.push(65);
s.push(17);
s.push(12);
s.display();
cout<<"Pop the items on stacks "<<s.pop()<<endl;
cout<<"Pop the items on stacks "<<s.pop()<<endl;
cout<<"Pop the items on stacks "<<s.pop()<<endl;
return 0;
}
Output is :
0 Comments