-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bag.cpp
195 lines (169 loc) · 6.29 KB
/
Bag.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "Bag.h"
#include "BagIterator.h"
#include <exception>
#include <iostream>
using namespace std;
//6.ADT Bag reprsentiert als ein dynamisches Array von eindeutigen Elementen (E) und ein
//dynamisches Array von Positionen (P) in E der Elemente des Bags. Zum Beispiel, das Bag [5, 10, -1, 2, 3, 10, 5, 5, -5] wird
//folgendermaen reprsentiert:U = [5, 10, -1, 2, 3, -5]P = [0, 1, 2, 3, 4, 1, 0, 0, 5]
//best case: θ(1) worst case:θ(1) average case:θ(1)
Bag::Bag() {
this->cap_list = 10;
this->cap_pos= 10;
this-> nrElemsListe = 0;
this-> liste = new TElem [cap_list];
this-> nrElemsPositions=0;
this-> positions= new int [cap_pos];
}
//best case:θ(1) worst case:θ(n) average case: allgemeine Komplexitat:O(n)
void Bag::add(TElem elem) {
int already_added=0;
for(int index=0;index<this->nrElemsListe;index+=1)
if(this->liste[index]==elem) {
//make space positions
if(this->nrElemsPositions == this->cap_pos){
this->cap_pos = this->cap_pos * 2;
int* new_array = new int[this->cap_pos];
for(int index_pos=0;index_pos<this->nrElemsPositions;index_pos+=1){
new_array[index_pos] = this->positions[index_pos];
}
delete[] this->positions;
this->positions = new_array;
}
this->positions[nrElemsPositions] = index;
nrElemsPositions+=1;
already_added = 1;
break;
}
if(already_added == 0) {
//make space elements
if (this->nrElemsListe == this->cap_list) {
this->cap_list = this->cap_list * 2;
int *new_array = new int[this->cap_list];
for (int index_list = 0; index_list < this->nrElemsListe; index_list += 1) {
new_array[index_list] = this->liste[index_list];
}
delete[] this->liste;
this->liste = new_array;
}
this->liste[nrElemsListe] = elem;
nrElemsListe += 1;
//make space positions
if (this->nrElemsPositions == this->cap_pos) {
this->cap_pos = this->cap_pos * 2;
int *new_array = new int[this->cap_pos];
for (int index_pos = 0; index_pos < this->nrElemsPositions; index_pos += 1) {
new_array[index_pos] = this->positions[index_pos];
}
delete[] this->positions;
this->positions = new_array;
}
this->positions[nrElemsPositions] = nrElemsListe - 1;
nrElemsPositions += 1;
}
}
//best case:θ(n) worst case:θ(n) average case: allgemeine Komplexitat:θ(n)
bool Bag::remove(TElem elem) {
int counter=0;
int pos_elem=INT_MIN;
int last_pos=INT_MIN;
for(int index=0;index<nrElemsListe;index+=1)
if(this->liste[index] == elem) {
pos_elem = index;
break;
}
for(int index=0;index<nrElemsPositions;index+=1)
if(this->positions[index] == pos_elem) {
counter += 1;
last_pos = index;
}
if(counter>1){
for(int index=last_pos;index<nrElemsPositions;index++){
this->positions[index] = this->positions[index+1];
}
nrElemsPositions--;
//make less space positions
if (this->nrElemsPositions < this->cap_pos / 4) {
this->cap_pos = this->cap_pos / 2;
int *new_array = new int[this->cap_pos];
for (int index_pos = 0; index_pos < this->nrElemsPositions; index_pos += 1) {
new_array[index_pos] = this->positions[index_pos];
}
delete[] this->positions;
this->positions = new_array;
}
return true;
}
else {
if (counter == 1) {
for (int index = last_pos; index < nrElemsPositions; index++) {
this->positions[index] = this->positions[index + 1];
}
nrElemsPositions--;
//make less space positions
if (this->nrElemsPositions < this->cap_pos / 4) {
this->cap_pos = this->cap_pos / 2;
int *new_array = new int[this->cap_pos];
for (int index_pos = 0; index_pos < this->nrElemsPositions; index_pos += 1) {
new_array[index_pos] = this->positions[index_pos];
}
delete[] this->positions;
this->positions = new_array;
}
for (int index = pos_elem; index < nrElemsListe; index++) {
this->liste[index] = this->liste[index + 1];
}
nrElemsListe--;
//make less space elements
if (this->nrElemsListe < this->cap_list / 4) {
this->cap_list = this->cap_list / 2;
int *new_array = new int[this->cap_list];
for (int index_list = 0; index_list < this->nrElemsListe; index_list += 1) {
new_array[index_list] = this->liste[index_list];
}
delete[] this->liste;
this->liste = new_array;
}
return true;
} else {
return false;
}
}
}
//best case:θ(1) worst case:θ(n) average case:θ(n) allgemeine Komplexitat:O(n)
bool Bag::search(TElem elem) const {
for(int index=0;index<this->nrElemsListe;index+=1)
if(this->liste[index] == elem)
return true;
return false;
}
//best case:θ(1) worst case:θ(n) average case:θ(n) allgemeine Komplexitat:O(n)
int Bag::nrOccurrences(TElem elem) const {
int counter=0;
int pos_elem = INT_MIN;
for(int index=0;index<nrElemsListe;index+=1)
if(this->liste[index] == elem)
pos_elem = index;
for(int index=0;index<nrElemsPositions;index+=1)
if(this->positions[index] == pos_elem)
counter+=1;
return counter;
}
//best case: θ(1) worst case:θ(1) average case:θ(1) allgemeine Komplexitat:θ(1)
int Bag::size() const {
return this->nrElemsPositions;
}
//best case: θ(1) worst case:θ(1) average case:θ(1) allgemeine Komplexitat:θ(1)
bool Bag::isEmpty() const {
if (nrElemsListe == 0)
return true;
return false;
}
BagIterator Bag::iterator() const {
return BagIterator(*this);
}
//best case: θ(1) worst case:θ(1) average case:θ(1) allgemeine Komplexitat:θ(1)
Bag::~Bag() {
delete[] liste;
delete[] positions;
}