-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoraha_StackArray.h
More file actions
71 lines (59 loc) · 1.85 KB
/
Loraha_StackArray.h
File metadata and controls
71 lines (59 loc) · 1.85 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
70
71
#ifndef LORAHA_STACKARRAY_H
#define LORAHA_STACKARRAY_H
#include <iostream>
using namespace std;
template <typename T>
class MyStackArrayType{
private:
T* stack = new T[1];
int stackSize = 1; //size of stack after adding an element... starts at 1, but that 1 is to know if its empty.
public:
MyStackArrayType(){
stack[0] = -1; //initialize stack with -1, if top is -1, then stack is empty.
}
void print(){
for (int i = 0; i < stackSize; i++){
cout << stack[i] << " ";
}
cout << endl;
}
int size(){
return stackSize - 1; //return size of stack, but -1 because of the -1 that is used to check if stack is empty.
}
void push(T data){
T* temp = new T[stackSize + 1];
for (int i = 0; i < stackSize; i++){
temp[i] = stack[i];
}
temp[stackSize] = data;
delete[] stack;
stack = temp;
stackSize++;
}
string is_empty(){
if (stackSize == 1){
return "True";
} else {
return "False";
}
}
T pop(){
if (stackSize != 1){
T* temp = new T[stackSize - 1];
for (int i = 0; i < stackSize - 1; i++){
temp[i] = stack[i];
}
T data = stack[stackSize - 1];
delete[] stack;
stack = temp;
stackSize--;
return data;
} else {
return -1; //stack is empty if -1 is returned. have the if statement here to not remove the -1 that is used to check if stack is empty.
}
}
T top(){
return stack[stackSize - 1];
}
};
#endif