-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBag.h
363 lines (318 loc) · 8.8 KB
/
Bag.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Bag.h
#ifndef _BAG_H_
#define _BAG_H_
#include "Node.h"
template<typename ItemType>
class BagInterface
{
public:
virtual int getCurrentSize() const = 0;
virtual bool isEmpty() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove(const ItemType& anEntry) = 0;
virtual void clear() = 0;
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
virtual void print() const = 0;
};
template<typename ItemType>
class LinkedBag : public BagInterface<ItemType>
{
private:
Node<ItemType>* headPtr; // Pointer to first node
int itemCount; // Current count of bag items
Node<ItemType>* getPointerTo(const ItemType& target) const;
// Returns either a pointer to the node containing a given entry
// or the null pointer if the entry is not in the bag.
public:
LinkedBag();
LinkedBag(const LinkedBag<ItemType>& aBag); // Copy constructor
virtual ~LinkedBag(); // Destructor should be virtual
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
void print() const;
};
template<typename ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private:
static const int DEFAULT_CAPACITY = 6;
ItemType* items;
int itemCount;
int maxItems;
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
ArrayBag(const ArrayBag<ItemType>& aBag);
~ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
void print() const;
const ArrayBag<ItemType>& operator=(const ArrayBag<ItemType>& aBag);
};
//=================================================================================================
// LinkedBag
//=================================================================================================
template<typename ItemType>
LinkedBag<ItemType>::LinkedBag() : headPtr(nullptr), itemCount(0)
{
}
template<typename ItemType>
LinkedBag<ItemType>::LinkedBag(const LinkedBag<ItemType>& aBag)
{
itemCount = aBag.itemCount;
Node<ItemType>* origChainPtr = aBag.headPtr; // Points to nodes in original chain
if (origChainPtr == nullptr)
headPtr = nullptr; // Original bag is empty; so is copy
else
{
// Copy first node
Node<ItemType>* newChainPtr = headPtr;
newChainPtr = new Node<ItemType>(origChainPtr->item);
// Copy remaining nodes
origChainPtr = origChainPtr->next;
while (origChainPtr != nullptr)
{
newChainPtr->next = new Node<ItemType>(origChainPtr->item);
newChainPtr = newChainPtr->next;
origChainPtr = origChainPtr->next;
}
newChainPtr->next = nullptr; // Flag end of new chain
}
}
template<typename ItemType>
LinkedBag<ItemType>::~LinkedBag()
{
clear();
}
template<typename ItemType>
int LinkedBag<ItemType>::getCurrentSize() const
{
return itemCount;
}
template<typename ItemType>
bool LinkedBag<ItemType>::isEmpty() const
{
return (itemCount == 0);
}
template<typename ItemType>
bool LinkedBag<ItemType>::add(const ItemType& newEntry)
{
// Add to beginning of chain: new node references rest of chain;
// (headPtr is null if chain is empty)
Node<ItemType>* newNodePtr = new Node<ItemType>();
newNodePtr->item = newEntry;
newNodePtr->next = headPtr; // New node points to chain
headPtr = newNodePtr; // New node is now first node
itemCount++;
return true;
}
template<typename ItemType>
bool LinkedBag<ItemType>::remove(const ItemType& anEntry)
{
Node<ItemType>* entryNodePtr = getPointerTo(anEntry);
bool canRemoveItem = (!isEmpty() && (entryNodePtr != nullptr));
if (canRemoveItem)
{
// Copy data from first node to located node
entryNodePtr->item = headPtr->item;
// Delete first node
Node<ItemType>* nodeToDeletePtr = headPtr;
headPtr = headPtr->next;
// Releasing the space to the system
delete nodeToDeletePtr;
nodeToDeletePtr = nullptr;
itemCount--;
}
return canRemoveItem;
}
template<typename ItemType>
void LinkedBag<ItemType>::clear()
{
Node<ItemType>* nodeToDeletePtr = headPtr;
while (headPtr != nullptr)
{
headPtr = headPtr->next;
// Releasing the space to the system
nodeToDeletePtr->next = nullptr;
delete nodeToDeletePtr;
nodeToDeletePtr = headPtr;
}
// headPtr is nullptr; nodeToDeletePtr is nullptr
itemCount = 0;
}
template<typename ItemType>
bool LinkedBag<ItemType>::contains(const ItemType& anEntry) const
{
return (getPointerTo(anEntry) != nullptr);
}
template<typename ItemType>
int LinkedBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
Node<ItemType>* curPtr = headPtr;
while (curPtr != nullptr)
{
if (anEntry == curPtr->item)
frequency++;
curPtr = curPtr->next;
}
return frequency;
}
template<typename ItemType>
void LinkedBag<ItemType>::print() const
{
Node<ItemType>* curPtr = headPtr;
for (int i = 0 ; i < itemCount ; i++) {
cout << curPtr->item << " ";
curPtr = curPtr->next;
}
cout << endl;
}
// private
// Returns either a pointer to the node containing a given entry
// or the null pointer if the entry is not in the bag.
template<typename ItemType>
Node<ItemType>* LinkedBag<ItemType>::getPointerTo(const ItemType& anEntry) const
{
bool found = false;
Node<ItemType>* curPtr = headPtr;
while (!found && (curPtr != nullptr))
{
if (anEntry == curPtr->item)
found = true;
else
curPtr = curPtr->next;
}
return curPtr;
}
//=================================================================================================
// ArrayBag
//=================================================================================================
template<typename ItemType>
ArrayBag<ItemType>::ArrayBag() : itemCount(0), maxItems(DEFAULT_CAPACITY)
{
items = new ItemType[DEFAULT_CAPACITY];
}
template<typename ItemType>
ArrayBag<ItemType>::ArrayBag(const ArrayBag<ItemType>& aBag) {
this->itemCount = aBag.itemCount;
this->maxItems = aBag.maxItems;
this->items = new ItemType[this->maxItems];
for (int i = 0 ; i < this->itemCount ; i++) {
this->items[i] = aBag.items[i];
}
}
template<typename ItemType>
ArrayBag<ItemType>::~ArrayBag()
{
delete[] items;
}
template<typename ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
}
template<typename ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return (itemCount == 0);
}
template<typename ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (!hasRoomToAdd)
{
ItemType* oldArray = items;
items = new ItemType[2 * maxItems];
for (int i = 0; i < maxItems; i++)
items[i] = oldArray[i];
delete[] oldArray;
maxItems *= 2;
}
items[itemCount] = newEntry;
itemCount++;
return true;
}
template<typename ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
}
template<typename ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
}
template<typename ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
}
template<typename ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0;
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
frequency++;
curIndex++;
}
return frequency;
}
template<typename ItemType>
void ArrayBag<ItemType>::print() const
{
for (int i = 0; i < itemCount; i++)
cout << items[i] << " ";
cout << endl;
}
template<typename ItemType>
const ArrayBag<ItemType>& ArrayBag<ItemType>::operator=(const ArrayBag<ItemType>& aBag)
{
if (this != &aBag) {
if (this->itemCount != aBag.itemCount) {
if (this->maxItems != aBag.maxItems) {
this->maxItems = aBag.maxItems;
delete[] this->items;
this->items = new ItemType[this->maxItems];
}
this->itemCount = aBag.itemCount;
}
for (int i = 0; i < this->itemCount; i++) {
this->items[i] = aBag.items[i];
}
}
return *this;
}
// private
template<typename ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const
{
for (int i = 0; i < itemCount; i++) {
if (items[i] == target)
return i;
}
return -1;
}
#endif // !_BAG_H_