Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Stack using array in C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int max,i,n;
int top=-1;
class stack
{
int s[20];
public:
void push();
void pop();
void display();
};
void stack::push()
{
int x;
if(top==(max-1))
cout<<"Stack is full";
else
{
cout<<"enter data:";
cin>>x;
top=top+1;
s[top]=x;
}}
void stack::pop()
{
if(top==-1)
cout<<"stack  is empty";
else
s[top--]='\0';
}void stack::display()
{cout<<"displaying the contents of stack:\n";
if(top==-1)
cout<<"stack is empty"<<"\n";
else
{for(i=0;i<=top;i++)
cout<<s[i]<<"\t";
cout<<"\n";
}}
void main()
{
stack sa;
clrscr();
cout<<"enter the range:";
cin>>max;
while(1)
{
cout<<"\n1.push\n2.pop\n3.display\n4.exit";
cout<<"\n enter ur choice:";
cin>>n;
switch(n)
{case 1:
sa.push();
break;
case 2:
sa.pop();
break;
case 3:
sa.display();
break;
case 4:
exit(0);
default:
cout<<"Invalid option";
}
}
getch();
}