-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
72 lines (66 loc) · 1.1 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
#include<iostream>
using namespace std;
const int MAX_SIZE = 100;
class stack{
private:
int top;
int items[MAX_SIZE];
public:
stack() :top(-1){};
void push(int element){
if (top >= MAX_SIZE - 1){
cout << "stack is full" << endl;
}
else{
top++;
items[top] = element;
}
}
bool isEmpty() {
if (top == -1)
return true;
else
return false;
};
void pop() {
if (isEmpty()){ cout << "stack is empty" << endl; }
else{ top--; };
}
void pop(int &element) {
if (isEmpty()){ cout << "stack is empty" << endl; }
else{
element = items[top];
top--;
};
};
int getTop(int &stacktop) {
if (isEmpty()){ cout << "stack is empty to get top" << endl; }
else
{
stacktop = items[top];
top--;
};
return stacktop;
};
void print() {
cout << "[";
for (int i = top; i >= 0; i--)
{
cout << items[i] << " ";
};
cout << "]";
cout << endl;
}
};
int main() {
stack s1;
s1.push(5);
s1.push(10);
s1.push(15);
s1.push(20);
s1.pop();
s1.push(7);
s1.print();
system("pause");
return 0;
};