-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.h
More file actions
140 lines (119 loc) · 2.74 KB
/
DynamicArray.h
File metadata and controls
140 lines (119 loc) · 2.74 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Made by Brycen Tanaka
#ifndef _DYNAMICARRAY_
#define _DYNAMICARRAY_
#include <cstdlib>
#include <iostream>
using namespace std;
class Darr{
public:
Darr() {
used_ = 0;
capacity_ = 0;
data_ = nullptr;
}
void insert(int in){
if (capacity_ == 0) {
capacity_++;
data_ = new int[capacity_];
} else if (capacity_ == used_) {
capacity_ *= 2;
int *temp = new int[capacity_];
for (int i = 0; i < used_; i++) {
temp[i] = data_[i];
}
delete [] data_;
data_ = temp;
}
data_[used_] = in;
used_++;
}
bool contains (int target){
for (int i = used_; i > 0; i++){
if (data_[i] == target){
return true;
}
}
return false;
}
void undolast(){
if(used_ > 0){
used_--;
}
}
int used(){
return used_;
}
int operator [](int target){
return data_[target];
}
void operator =(Darr rhs){
data_ = rhs.data_;
capacity_ = rhs.capacity_;
used_ = rhs.used_;
}
private:
int* data_;
int capacity_;
int used_;
};
class LDarr{
public:
LDarr() {
used_ = 0;
capacity_ = 0;
data_ = nullptr;
}
void insert(long in){
if (capacity_ == 0) {
capacity_++;
data_ = new long[capacity_];
} else if (capacity_ == used_) {
capacity_ *= 2;
long *temp = new long[capacity_];
for (int i = 0; i < used_; i++) {
temp[i] = data_[i];
}
delete [] data_;
data_ = temp;
}
data_[used_] = in;
used_++;
}
bool contains (long target){
for (int i = used_; i > 0; i--){
if (data_[i] == target){
return true;
}
}
return false;
}
bool twoinarow(long one, long two){
for (int i = 0; i < used_ - 1; i += 2){
if (data_[i] == one && data_[i + 1] == two){
return true;
}
}
return false;
}
void undolast(){
if(used_ > 0){
used_--;
}
}
int used(){
return used_;
}
int operator [](int target){
return data_[target];
}
void operator =(LDarr rhs){
data_ = rhs.data_;
capacity_ = rhs.capacity_;
used_ = rhs.used_;
}
private:
long* data_;
int capacity_;
int used_;
};
#endif // _DYNAMICARRAY_