-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
65 lines (63 loc) · 1.13 KB
/
Copy pathstack.cpp
File metadata and controls
65 lines (63 loc) · 1.13 KB
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
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
template <class t>
class stack {
private:
int top;
t item[MAX_SIZE];
public:
stack()
{
top = -1;
}
void push(t val)
{
if (top >= MAX_SIZE - 1)
{
cout << "stack full ";
}
else {
top++;
item[top] = val;
}
}
bool isempty()
{
return top < 0;
}
void pop() {
if (isempty()) {
cout << "stack empty on pop";
}
else {
top--;
}
}
void gettop(t&stacktop) {
if (isempty()) {
cout << "stack empty on pop";
}
else {
stacktop = item[top];
cout << stacktop << endl;
}
}
void print() {
cout << "[";
for (int i = top; i >= 0; i--) {
cout << item[i] << " ";
}
cout << "]";
cout << endl;
}
};
int main()
{
stack<int> s;
s.push(5);
s.push(10);
s.push(17);
s.push(20);
s.print();
}