-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlb-01-implement-stack.cpp
More file actions
69 lines (67 loc) · 1.21 KB
/
Copy pathlb-01-implement-stack.cpp
File metadata and controls
69 lines (67 loc) · 1.21 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
66
67
68
69
#include <iostream>
#include <vector>
using namespace std;
class Stack {
public:
int top;
int capacity;
int *arr;
Stack(int cap = 10) {
capacity = cap;
top = -1;
arr = new int[capacity];
}
int size() {
return top + 1;
}
bool empty() {
return size() == 0;
}
int full() {
return size() == capacity;
}
bool insert(int val) {
if (!full()) {
arr[++top] = val;
return true;
} else {
cout << "Overflow\n";
return false;
}
}
int peek() {
if (!empty())
return arr[top];
cout << "Empty\n";
return -1;
}
void pop() {
if (!empty())
top--;
else
cout << "Underflow\n";
}
void show() {
for (int i = 0; i < size(); i++) {
cout << "|" << arr[i] << " ";
}
cout << endl;
}
};
int main() {
Stack s = Stack(4);
s.show();
s.insert(4);
s.show();
s.pop();
s.show();
s.pop();
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
s.insert(5);
cout << s.peek() << endl;
s.show();
return 0;
}