#include<iostream>
using namespace std;
class stack
{
int a[10],top,del;
public:
stack()
{
top=-1;
}
stack operator+(int);
stack operator--();
friend ostream & operator<<(ostream &,stack &);
}s1;
stack stack::operator+(int item)
{
if(s1.top>=5)
cout<<"stack full\n";
else
a[++s1.top]=item;
return s1;
}
stack stack::operator--()
{
if(s1.top==-1)
{
cout<<"stack empty\n";
exit(0);
}
else
{
del=a[s1.top--];
cout<<"\nitem deleted\t"<<del<<"\n";
}
return s1;
}
ostream &operator<<(ostream &out,stack &s1)
{
if(s1.top==-1)
{
cout<<"stack empty\n";
exit(0);
}
else
{
for(int i=0;i<=s1.top;i++)
cout<<s1.a[i]<<"\t";
}
return out;
}
int main()
{
int n,ch;
system("clear");
for(;;)
{
cout<<"\n1.push\n2.pop\n3.display\nenter u r choice\t";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nenter item to insert\t";
cin>>n;
s1=s1+n;
break;
case 2:
s1=--s1;
break;
case 3:
cout<<"\n\nstack contents\n"<<s1;
break;
default:exit(0);
}
}
return 0;
}
using namespace std;
class stack
{
int a[10],top,del;
public:
stack()
{
top=-1;
}
stack operator+(int);
stack operator--();
friend ostream & operator<<(ostream &,stack &);
}s1;
stack stack::operator+(int item)
{
if(s1.top>=5)
cout<<"stack full\n";
else
a[++s1.top]=item;
return s1;
}
stack stack::operator--()
{
if(s1.top==-1)
{
cout<<"stack empty\n";
exit(0);
}
else
{
del=a[s1.top--];
cout<<"\nitem deleted\t"<<del<<"\n";
}
return s1;
}
ostream &operator<<(ostream &out,stack &s1)
{
if(s1.top==-1)
{
cout<<"stack empty\n";
exit(0);
}
else
{
for(int i=0;i<=s1.top;i++)
cout<<s1.a[i]<<"\t";
}
return out;
}
int main()
{
int n,ch;
system("clear");
for(;;)
{
cout<<"\n1.push\n2.pop\n3.display\nenter u r choice\t";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nenter item to insert\t";
cin>>n;
s1=s1+n;
break;
case 2:
s1=--s1;
break;
case 3:
cout<<"\n\nstack contents\n"<<s1;
break;
default:exit(0);
}
}
return 0;
}
No comments:
Post a Comment