-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. Stack.cpp
95 lines (92 loc) · 1.65 KB
/
6. Stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
using namespace std;
const int SIZE=5;
class stack
{
int items[SIZE];
int top;
int full();
int empty();
public:
stack()
{
top=-1;
}
stack operator--(int);
friend stack operator+(stack s1,int elem);
friend ostream &operator<<(ostream &os,stack &s1);
};
int stack::full()
{
if(top==SIZE-1)
return 1;
else
return 0;
}
int stack::empty()
{
if(top==-1)
return 1;
else
return 0;
}
stack stack::operator--(int )
{
if(empty())
{
cout<<"Stack underflow\n";
}
else
{
cout<<"\nThe element deleted is :"<<items[top];
stack t;
t.top=top--;
for(int i=0;i<=top;i++)
t.items[i]=items[i];
}
return *this;
}
ostream &operator<<(ostream &os,stack &s1)
{
for(int i=s1.top;i>=0;i--)
{
os<<s1.items[i]<<"\n";
}
return os;
}
stack operator+(stack s1,int elem)
{
if(s1.full())
cout<<"\nStack overflow\n";
else
s1.items[++(s1.top)]=elem;
return s1;
}
int main()
{
stack s1;
int choice,elem;
for(;;)
{
cout<<"\n1:PUSH 2:POP 3:DISPLAY 4:EXIT\n"
<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>elem;
s1=s1+elem;
break;
case 2:
s1=s1--;
break;
case 3:
cout <<"The contents of the stack are :\n"<<s1;
break;
case 4: exit(0);
default: cout <<"Invalid choice\n";
exit(0);
}
}
}