-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
119 lines (99 loc) · 2.19 KB
/
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<bits/stdc++.h>
using namespace std;
#define SIZE 5
using namespace std;
class STACK
{
private:
int num[SIZE];
int top;
public:
STACK(); //defualt constructor
int push(int);
int pop();
int isEmpty();
int isFull();
void displayItems();
};
STACK::STACK(){
top=-1;
}
int STACK::isEmpty(){
if(top==-1)
return 1;
else
return 0;
}
int STACK::isFull(){
if(top==(SIZE-1))
return 1;
else
return 0;
}
int STACK::push(int n){
//check stack is full or not
if(isFull()){
return 0;
}
++top;
num[top]=n;
return n;
}
int STACK::pop(){
//to store and print which number
//is deleted
int temp;
//check for empty
if(isEmpty())
return 0;
temp=num[top];
--top;
return temp;
}
void STACK::displayItems(){
int i; //for loop
cout<<"STACK is: ";
for(i=(top); i>=0; i--)
cout<<num[i]<<" ";
cout<<endl;
}
int main(){
//declare object
STACK stk;
int choice, n,temp;
do
{
cout<<endl;
cout<<"0 - Exit."<<endl;
cout<<"1 - Push Item."<<endl;
cout<<"2 - Pop Item."<<endl;
cout<<"3 - Display Items (Print STACK)."<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 0: break;
case 1:
cout<<"Enter item to insert: ";
cin>>n;
temp=stk.push(n);
if(temp==0)
cout<<"STACK is FULL."<<endl;
else
cout<<temp<<" inserted."<<endl;
break;
case 2:
temp=stk.pop();
if(temp==0)
cout<<"STACK IS EMPTY."<<endl;
else
cout<<temp<<" is removed (popped)."<<endl;
break;
case 3:
stk.displayItems();
break;
default:
cout<<"An Invalid choice."<<endl;
}
}while(choice!=0);
return 0;
}