-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstack.cpp
More file actions
69 lines (59 loc) · 1.1 KB
/
stack.cpp
File metadata and controls
69 lines (59 loc) · 1.1 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>
using namespace std;
template <typename T>
class stack {
public:
T *arr;
int top;
size_t capacity;
size_t size;
stack (size_t capacity) {
this->capacity = capacity;
arr = new T[capacity];
top = -1;
size = 0;
}
void push(T data) {
if (size < capacity) {
arr[++top] = data;
++size;
} else {
T *arr2 = new T[capacity * 2];
for (int i = 0; i < capacity; ++i) {
arr2[i] = arr[i];
}
arr2[++top] = data;
++size;
capacity *= 2;
delete []arr;
arr = arr2;
}
}
void pop() {
if (is_empty()) {
cout << "Stack is empty" << endl;
return;
}
--top;
--size;
}
T peek() {
if (is_empty()) {
exit(-1);
}
return arr[top];
}
bool is_empty() {
if (size != 0) {
return false;
}
return true;
}
~stack() {
delete []arr;
}
};
int main()
{
return 0;
}