Implementation of Stacks

"Implementation of Stacks in Array through classes in C++"


#include<iostream.h>
#include<conio.h>
class stack{
private:
int top,max;
int *stackarr;
public:
stack()
{
cout<<"\nEnter the size of stack: ";
cin>>max;
top=-1;
int *stackarr=new int[max];
}
void push(int n)
{
if(top==max)
cout<<"\nStack id full\n";
else
top=top+1;
stackarr[top]=n;
}
void pop()
{
if(top==-1)
cout<<"\nStack is empty\n";
else
cout<<"\nThe stack element "<<stackarr[top]<<" is poped\n";
top=top-1;
}
void display()
{

for(int i=0;i<=top;i++)
cout<<"\nvalue is :"<<stackarr[i];
}
};
void main()
{
clrscr();
int a=0,value=0;
stack obj;
while(getch()!=27)
{
cout<<"\n \n\n\t\tSelect an option:\n 1   for Push\n 2   for Pop\n 3   for Display ";
cin>>a;
clrscr();
switch(a)
{
case 1:
{
cout<<"\nEnter value for index: ";
cin>>value;
obj.push(value);
break;
}
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
}
cout<<"\n\n\t   || Press Esc Key to exit || Any Key For next Operation  ||";
}
}

0 comments:

Post a Comment

My Instagram