-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedAList.h
More file actions
141 lines (115 loc) · 3.02 KB
/
Copy pathSortedAList.h
File metadata and controls
141 lines (115 loc) · 3.02 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
141
#ifndef __SORTED_ALIST_H__
#define __SORTED_ALIST_H__
#include "SortedList.h"
// use factor 2
#define RFACTOR 2
namespace ds {
template <typename ItemType> class TestDriver; // for autograding; please ignore
/** Array-based sorted list. */
template <typename ItemType> class SortedAList : public SortedList<ItemType> {
friend class TestDriver<ItemType>; // for autograding; please ignore
private:
/** The underlying array. */
ItemType *items;
/** Stores the current size of the list. */
int count;
/** Max number of items allowed. */
int maxCnt;
/** Resize the underlying array to the target capacity. */
void resize(int capacity) {
maxCnt = capacity;
ItemType *a = new ItemType[maxCnt];
for (int i = 0; i < count; i++) {
a[i] = items[i];
}
delete[] items;
items = a;
}
public:
/**
* Construct a new SortedAList object.
*
* @param initSize initial size of the underlying array; default 100
*/
explicit SortedAList(int initSize = 100) {
count = 0;
maxCnt = initSize;
items = new ItemType[maxCnt];
}
/** Destroy the SortedAList object. */
~SortedAList() { delete[] items; }
/** Return the number of elements in the sorted list. */
int size() const override { return count; }
/** Return the i-th item in the sorted list .*/
ItemType get(int i) const override { return items[i]; }
/** Return the smallest item. */
ItemType smallest() const override { return items[0]; }
/** Return the greatest item. */
ItemType greatest() const override { return items[count - 1]; }
inline ItemType remove(int i) override;
inline void put(const ItemType &it) override;
};
/**
* Remove and return the i-th item from the sorted list.
*/
template <typename ItemType> ItemType SortedAList<ItemType>::remove(int i) {
// store the value of what is being removed
ItemType value = items[i];
// move the numbers above the index 'i' down
for (int j = i; j < count -1; j++) {
items[j] = items[j + 1];
}
// decrease count
count--;
// return what was removed
return value;
}
/**
* Put "it" into the "items" array while ensuring that "items" are sorted.
*/
template <typename ItemType>
void SortedAList<ItemType>::put(const ItemType &it) {
// resize if neccesary
if (count == maxCnt) {
resize(count * 2);
}
int i = 0;
// see if the list is empty
if (count == 0)
{
items[0] = it;
}
// see if it is the smallest (aka first)
else if (it < items[i])
{
for (int h = count; h > 0; h--)
{
items[h] = items[h-1];
}
items[0] = it;
}
// see if it is the largest value
else if (it > items[count - 1]) {
items[count] = it;
}
// else, the value is somewhere in between
else
{
// increment through to find where it belongs
while (items[i] < it)
{
i++;
}
// move the other items up one
for (int j = count; j > i; j--)
{
items[j] = items[j - 1];
}
// place it
items[i] = it;
}
// increment count
count++;
}
} // namespace ds
#endif // __SORTED_ALIST_H__