-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylist.h
209 lines (183 loc) · 4.53 KB
/
mylist.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
#ifndef MYLIST_H
#define MYLIST_H
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
const int DEFAULT_LIST_SIZE = 100;
template <typename T>
class MyList {
public :
MyList();
MyList(int capacity);
~MyList();
void push_back(T newval);
T& at(int loc); // Q : difference between T& at() and T &at().
int size() const;
bool remove(T val); // If "val" occurs in the list, remove the first
// occurence of "val" and return true. Return false
// otherwise
T pop(int loc); // remove and return the value at location "loc"
T& operator[](int loc);
T max() const; // return maximum valuse
T min() const; // return minimum valuse
void pop_back();
private :
T *data;
int size_;
int capacity_;
};
/** Default constructor
* Initialize capacity, size, and date
*/
template <typename T>
MyList<T>::MyList()
{
capacity_ = DEFAULT_LIST_SIZE;
size_ = 0;
data = new T[capacity_];
}
/** Constructor
* Initialize capacity, size, and date when capacity is given
*/
template <typename T>
MyList<T>::MyList(int capacity)
{
capacity_ = capacity;
size_ = 0;
data = new T[capacity_];
}
/** Destructor */
template <typename T>
MyList<T>::~MyList()
{
delete [] data;
}
/** Push back new value to the list
* @param newval New value that will be inserted at the back of list
* @return Nothing
*/
template <typename T>
void MyList<T>::push_back(T newval)
{
data[size_] = newval;
size_++;
// when array is full with data -> double the size of it
if(size_ == capacity_){
T *tempData = new T[capacity_];
for(int i=0;i<capacity_;i++){
tempData[i] = data[i];
}
capacity_ *= 2; // double the size of array
delete [] data;
data = new T[capacity_];
for(int i=0;i<(capacity_/2);i++){
data[i] = tempData[i];
}
delete [] tempData;
}
}
/** Return the value at particular location
* @param loc Loaction number
* @return Value at the location
*/
// exception : 1. if loc is bigger or smaller than the current size
template <typename T>
T& MyList<T>::at(int loc)
{
if(loc > (size_-1) || loc < 0) throw out_of_range("Out of range!");
// if I use 'try' and 'catch' statement inside of this function...
/*
try{
if(loc > (size_-1) || loc < 0) throw out_of_range("Out of range!");
}
catch(const std::exception& err){
cerr<< err.what() << endl;
}
*/
// it won't work unless i use loop statement in order to get the proper range
// because this function needs to return the proper valuse to the main function
// if a funtion is void type -> it might be ok
return data[loc];
}
/** Return the size of mylist
* @return Size of mylist
*/
template <typename T>
int MyList<T>::size() const
{
return size_;
}
/** Remove particular value if that value is in the list. \n
* Return true if that value exist, and false if does not exist.
* @param val Value that will be deleted if the same value is in the list
* @return True or False according to the result
*/
template <typename T>
bool MyList<T>::remove(T val)
{
bool flag = false;
for(int i=0;i<size_;i++){
if(data[i] == val){
flag = true;
pop(i);
}
}
return flag;
}
/** Return the value at particular location and remove it
* @param loc Location that will be returned and deleted
* @return Value at entered location
*/
// exception : 1. if loc is bigger than the current size
template <typename T>
T MyList<T>::pop(int loc)
{
T temp;
if(loc > (size_-1) || loc < 0) throw out_of_range("Out of range!");
temp = data[loc];
for(int i=loc;i<size_;i++){
data[i] = data[i+1];
}
size_--;
return temp;
}
/** Operater for [] */
// add "&" in return type as a reference variable -> can make a change as well, instead of just printing
template <typename T>
T& MyList<T>::operator[](int loc)
{
return at(loc); // it already has a throw for exception case
}
/** Return the biggest value in the list
* @return Maximum value
*/
template <typename T>
T MyList<T>::max() const
{
T max = data[0];
for(int i=0;i<size_;i++){
if(max < data[i]) max = data[i];
}
return max;
}
/** Return the smallest value in the list
* @return Smallest value
*/
template <typename T>
T MyList<T>::min() const
{
T min = data[0];
for(int i=0;i<size_;i++){
if(min > data[i]) min = data[i];
}
return min;
}
/** Delete the most back of list */
template <typename T>
void MyList<T>::pop_back()
{
pop(size_-1);
}
#endif