#include<iostream.h>
#include<conio.h>
class stack
{
int a[30],i,n;
public:
void head();
void push();
void pop();
void view();
};
void stack::head()
{
i=0;
cout<<"implementation of stack using array"<<endl;
cout<<"***********************************"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"3.view"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter the array:";
cin>>n;
}
void stack::push()
{
if(i>n)
{
cout<<"stack is full"<<endl;
}
else
{
cout<<"enter the num:";
cin>>a[i];
i++;
}
}
void stack::pop()
{
if(i==0)
{
cout<<"stack is empty"<<endl;
}
else
{
i--;
cout<<"deleted elements is:"<<a[i]<<endl;
}
}
void stack::view()
{
cout<<"the result is:"<<endl;
for(int j=0;j<i;j++)
cout<<[j]<<endl;
}
void main()
{
int a;
stack c;
clrscr();
c.head();
do
{
cout<<"enter your choice:";
cin>>(a)
{
case 1:
c.push();
break;
case 2:
c.pop();
break;
case 3:
c.view();
break;
case 4:
cout<<"exit"<<endl;
break;
}
}
while(a!=4);
getch();
}
Output:
1.push
2.pop
3.view
4.exit
enter the array:2
enter your choice:1
enter the num:10
enter your choice:1
enter the num:20
enter your choice:1
stack is full
enter your choice:3
the result is:
10
20
enter your choice:2
deleted elements is:20
enter your choice:2
deleted elements is:10
enter your choice:2
stack is empty
enter your choice:4
exit
#include<conio.h>
class stack
{
int a[30],i,n;
public:
void head();
void push();
void pop();
void view();
};
void stack::head()
{
i=0;
cout<<"implementation of stack using array"<<endl;
cout<<"***********************************"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"3.view"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter the array:";
cin>>n;
}
void stack::push()
{
if(i>n)
{
cout<<"stack is full"<<endl;
}
else
{
cout<<"enter the num:";
cin>>a[i];
i++;
}
}
void stack::pop()
{
if(i==0)
{
cout<<"stack is empty"<<endl;
}
else
{
i--;
cout<<"deleted elements is:"<<a[i]<<endl;
}
}
void stack::view()
{
cout<<"the result is:"<<endl;
for(int j=0;j<i;j++)
cout<<[j]<<endl;
}
void main()
{
int a;
stack c;
clrscr();
c.head();
do
{
cout<<"enter your choice:";
cin>>(a)
{
case 1:
c.push();
break;
case 2:
c.pop();
break;
case 3:
c.view();
break;
case 4:
cout<<"exit"<<endl;
break;
}
}
while(a!=4);
getch();
}
Output:
1.push
2.pop
3.view
4.exit
enter the array:2
enter your choice:1
enter the num:10
enter your choice:1
enter the num:20
enter your choice:1
stack is full
enter your choice:3
the result is:
10
20
enter your choice:2
deleted elements is:20
enter your choice:2
deleted elements is:10
enter your choice:2
stack is empty
enter your choice:4
exit
No comments:
Post a Comment