-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSortedList.h
More file actions
126 lines (105 loc) · 2.04 KB
/
SortedList.h
File metadata and controls
126 lines (105 loc) · 2.04 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
//This class isn't separated into two files to avoid reference error
//when compiling from command line
#ifndef SortedList_H_INCLUDED
#define SortedList_H_INCLUDED
#include<iostream>
using namespace std;
//T is the ItemType
template <class T>
class SortedList {
private:
T* data;
int length;
int maxSize;
int currentPos;
public:
SortedList();
SortedList(int);
~SortedList();
void InsertItem(T);
bool GetNextItem(T&);
void ResetList();
bool IsFull();
void PrintAll();
int GetLength();
void DeleteAll();
};
///Function Definitions:
template <class T>
SortedList<T>::SortedList(int size) {
maxSize = size;
data = new T[maxSize];
length = 0;
currentPos = -1;
}
template <class T>
SortedList<T>::SortedList() {
maxSize = 3;
data = new T[maxSize];
length = 0;
currentPos = -1;
}
template <class T>
void SortedList<T>::InsertItem(T item) {
int location = 0;
bool moreToSearch = (location<length);
if(IsFull()){
return;
//If list is already full, returns without any signal
}
while (moreToSearch) {
if( item > data[location] ){
location++;
moreToSearch = (location<length);
}
else{
moreToSearch = false;
}
}
for (int index = length; index>location; index--) {
data[index] = data[index - 1];
}
data[location] = item;
length++;
}
template <class T>
SortedList<T>::~SortedList() {
delete[] data;
}
template <class T>
bool SortedList<T>::IsFull(){
return length==maxSize;
}
template <class T>
void SortedList<T>::ResetList() {
currentPos = -1;
}
template <class T>
bool SortedList<T>::GetNextItem(T& item) {
if(currentPos>=maxSize){
return false;
//if currentPosition exceeds maximum number of elements
}
currentPos++;
item = data[currentPos];
return true;
}
template <class T>
void SortedList<T>::PrintAll() {
for(int i=0; i<maxSize; i++){
cout<<data[i]<<" "<<endl;
}
cout<<endl;
}
template <class T>
int SortedList<T>::GetLength() {
return length;
}
template <class T>
void SortedList<T>::DeleteAll() {
delete[] data;
data = new T[maxSize];
length = 0;
currentPos = -1;
}
#endif // SortedList_H_INCLUDED