#include<iostream.h>
#include<conio.h>
class stack
{
int *a,i,n;
public:
void head();
void push();
void pop();
void view();
};
void stack::head()
{
i=0;
cout<<"implementation of stack using pointer"<<endl;
cout<<"*************************************"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"3.view"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter the value of n:";
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<<"(a+j)<<endl;
}
void main()
{
int a;
stack *c;
clrscr();
c->head();
do
{
cout<<"enter your choice:";
cin>>a;
switch(a)
{
case 1:
c->push();
break;
case 2:
c->pop();
break;
case 3:
c->view();
break;
case 4:
cout<<"exit";
break;
}
}
while(a!=4);
getch();
}
Output:
1.push
2.pop
3.view
4.exit
enter the value of n:2
enter your choice:1
enter the num:100
enter your choice:1
enter the num:200
enter your choice:1
stack is full
enter your choice:3
the result is
100
200
enter your choice:2
deleted elements is 200
enter your choice:2
deleted elements is 100
enter your choice:2
stack is empty
enter your choice:4
exit
#include<conio.h>
class stack
{
int *a,i,n;
public:
void head();
void push();
void pop();
void view();
};
void stack::head()
{
i=0;
cout<<"implementation of stack using pointer"<<endl;
cout<<"*************************************"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"3.view"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter the value of n:";
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<<"(a+j)<<endl;
}
void main()
{
int a;
stack *c;
clrscr();
c->head();
do
{
cout<<"enter your choice:";
cin>>a;
switch(a)
{
case 1:
c->push();
break;
case 2:
c->pop();
break;
case 3:
c->view();
break;
case 4:
cout<<"exit";
break;
}
}
while(a!=4);
getch();
}
Output:
1.push
2.pop
3.view
4.exit
enter the value of n:2
enter your choice:1
enter the num:100
enter your choice:1
enter the num:200
enter your choice:1
stack is full
enter your choice:3
the result is
100
200
enter your choice:2
deleted elements is 200
enter your choice:2
deleted elements is 100
enter your choice:2
stack is empty
enter your choice:4
exit
No comments:
Post a Comment