Stack Using Array

#include<iostream.h>
#include<conio.h>

class stack
{
private:
int ele[10];
int top;
public:
stack();//Constructor
void push(int);
int pop();
int underflow();
int overflow();
int peep();
};
stack::stack()
{

top=-1;

}
int stack::underflow()
{
if(top==-1)
return(1);
else
return(0);
}
int stack::overflow()
{
if(top==9)
return(1);
else
return(0);
}
void stack::push(int n)
{
if(overflow()==1)
cout<<“\nStack is Full”;
else
{
top=top+1;
ele[top]=n;
}
}
int stack::pop()
{
int info=0;
if(underflow()==1)
{
cout<<“Stack is Empty.”;
}
else
{
info=ele[top];
top=top-1;
}
return(info);
}
int stack::peep()
{
int info=0;
if(underflow()==1)
cout<<“Stack is Empty.”;
else
{
info=ele[top];
}
return(info);
}
int main()
{
stack s;
char st;
int n,c;
clrscr();
do
{
cout<<“\n1.Push”;
cout<<“\n2.Pop”;
cout<<“\n3.Peep”;
cout<<“\nPlease enter your choice: “;
cin>>c;

switch(c)
{
case 1:
cout<<“\nEnter the value to push: “;
cin>>n;
s.push(n);
break;
case 2:
n=s.pop();
cout<<“\nRemoved value is “<<n;
break;
case 3:
n=s.peep();
cout<<“\nTop Most Element is “<<n;
break;

};
cout<<“\nDo you want to continue: “;
cin>>st;
}while(st==’Y’||st==’y’);
getch();
return(0);
}
/*
OUTPUT:
——-
1.Push
2.Pop
3.Peep
Please enter your choice: 1

Enter the value to push: 5

Do you want to continue: y

1.Push
2.Pop
3.Peep
Please enter your choice: 1

Enter the value to push: 10

Do you want to continue: y

1.Push
2.Pop
3.Peep
Please enter your choice: 3

Top Most Element is 10
Do you want to continue: n
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.