-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
77 lines (62 loc) · 1.13 KB
/
stack.h
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
#ifndef STACK_H
#define STACK_H
#include "ArduinoExtras.h"
struct Stack {
/** Index of the top item, or -1 if stack is empty. */
int topIdx;
/** Dynamically allocated array */
int *arr;
char *init_1;
char *init_2;
char *init_3;
/** Size of arr */
int arrSize;
Stack() {
topIdx = -1;
arr = new int[11];
init_1 = new char[11];
init_2 = new char[11];
init_3 = new char[11];
arrSize = 11;
}
~Stack() {
delete[] arr;
}
bool isEmpty() {
return topIdx < 0;
}
bool isFull() {
return topIdx == 10;
}
int numItems() {
return topIdx;
}
int top() {
assert(!isEmpty());
return arr[topIdx];
}
char initial1() {
assert(!isEmpty());
return init_1[topIdx];
}
char initial2() {
assert(!isEmpty());
return init_2[topIdx];
}
char initial3() {
assert(!isEmpty());
return init_3[topIdx];
}
void pop() {
assert(!isEmpty());
topIdx--;
}
void push(int item, char init1, char init2, char init3) {
topIdx++;
arr[topIdx] = item;
init_1[topIdx] = init1;
init_2[topIdx] = init2;
init_3[topIdx] = init3;
}
};
#endif