-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.cpp
319 lines (297 loc) · 5.43 KB
/
List.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
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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
template <typename T>
struct ListItem
{
T Data;
int Index;
ListItem * Next;
ListItem(int index, int data, ListItem * next = NULL)
{
Index = index;
Data = data;
Next = next;
}
};
template <typename T>
class List
{
private:
int _itemsCount;
ListItem<T> * _first;
ListItem<T> * _last;
public:
List();
~List();
bool Add(int index, T data);
ListItem<T>* Get(int index);
bool Del(int index);
bool DelFirst();
bool DelLast();
void AddLast(T data);
void AddFirst(T data);
void ChangeIndex(ListItem<T>* item, bool plus);
};
/** List::List() */
template <typename T>
List<T>::List()
{
_itemsCount = 0;
_first = _last = NULL;
}
/** List::~List() */
template <typename T>
List<T>::~List()
{
ListItem<T> * current = NULL;
ListItem<T> * next = _first;
while (next)
{
current = next;
next = next->Next;
delete current;
}
}
/** bool List::Add(int index, int data) */
template <typename T>
bool List<T>::Add(int index, T data)
{
if (index < 0 || index > _itemsCount)
return false;
if (index == 0)
AddFirst(data);
else if (index == _itemsCount)
AddLast(data);
else
{
ListItem<T> * prev = Get(index - 1);
ListItem<T> * newItem = new ListItem<T>(index, data, prev->Next);
prev->Next = newItem;
_itemsCount++;
ChangeIndex(newItem->Next, true);
}
return true;
}
/** ListItem* List::Get(int index) */
template <typename T>
ListItem<T>* List<T>::Get(int index)
{
if (index < 0 || index >= _itemsCount)
throw "Bad index";
ListItem<T> *current = _first;
while (current->Index < index)
current = current->Next;
return current;
}
/** bool List::Del(int index) */
template <typename T>
bool List<T>::Del(int index)
{
if (index < 0 || index >= _itemsCount)
return false;
if (index == 0)
DelFirst();
else if (index == _itemsCount - 1)
DelLast();
else
{
ListItem<T> * prev = Get(index - 1);
ListItem<T> * next = prev->Next->Next;
delete prev->Next;
prev->Next = next;
_itemsCount--;
ChangeIndex(next, false);
}
return true;
}
/** bool List::DelFirst() */
template <typename T>
bool List<T>::DelFirst()
{
if (_first == NULL)
return false;
if (_itemsCount == 1)
{
delete _first;
_first = NULL;
_last = NULL;
}
else
{
ListItem<T> * current = _first;
_first = _first->Next;
delete current;
ChangeIndex(_first, false);
}
_itemsCount--;
return true;
}
/** bool List::DelLast() */
template <typename T>
bool List<T>::DelLast()
{
if (_last == NULL)
return false;
if (_itemsCount == 1)
{
delete _first;
delete _last;
_first = _last = NULL;
}
else
{
_last = Get(_itemsCount - 2);
delete _last->Next;
_last->Next = NULL;
}
_itemsCount--;
return true;
}
/** void List::AddFirst(int data) */
template <typename T>
void List<T>::AddFirst(T data)
{
if (_first == NULL)
_first = _last = new ListItem<T>(0, data);
else
{
_first = new ListItem<T>(0, data, _first);
ChangeIndex(_first->Next, true);
}
_itemsCount++;
}
/** void List::AddLast(int data) */
template <typename T>
void List<T>::AddLast(T data)
{
if (_first == NULL)
_first = _last = new ListItem<T>(0, data);
else
{
_last->Next = new ListItem<T>(_last->Index + 1, data);
_last = _last->Next;
_last->Next = NULL;
}
_itemsCount++;
}
/** void List::ChangeIndex(ListItem* item, bool plus) */
template <typename T>
void List<T>::ChangeIndex(ListItem<T>* item, bool plus)
{
if (plus)
while (item)
{
item->Index++;
item = item->Next;
}
else
while (item)
{
item->Index--;
item = item->Next;
}
}
/** vector<string> &split(const string &s, char delim, vector<string> &elems) */
vector<string> &split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while (getline(ss, item, delim))
elems.push_back(item);
return elems;
}
/** vector<string> &split(const string &s, char delim, vector<string> &elems) */
vector<string> split(const string &s, char delim)
{
vector<string> elems;
split(s, delim, elems);
return elems;
}
/** int main(int argc, char* argv[]) */
int main(int argc, char* argv[])
{
List<int> list;
int N;
cout << "N = ";
cin >> N;
vector<string> expressions;
string expression;
int i = 0;
int size;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while (i < N && i >= 0)
{
cout << i + 1 << ">>";
getline(cin, expression);
expressions = split(expression, ' ');
size = expressions.size();
if (size == 0)
{
cout << "The expression is not correct. Try again.\n";
i--;
}
else if (expressions[0] == "add")
{
if (size == 3)
{
if (!list.Add(stoi(expressions[1]), stoi(expressions[2])))
{
cout << "Bad index. Try again\n";
i--;
}
}
else
{
cout << "The expression is not correct. Try again.\n";
i--;
}
}
else if (expressions[0] == "del")
{
if (size == 2)
{
if (!list.Del(stoi(expressions[1])))
{
cout << "Bad index. Try again\n";
i--;
}
}
else
{
cout << "The expression is not correct. Try again.\n";
i--;
}
}
else if (expressions[0] == "get")
{
if (size == 2)
{
try
{
cout << list.Get(stoi(expressions[1]))->Data << endl;
}
catch (char* message)
{
cout << message << ". Try again\n";
i--;
}
}
else
{
cout << "The expression is not correct. Try again.\n";
i--;
}
}
else
{
cout << "The operation is not correct. Try again.\n";
i--;
}
i++;
}
system("pause");
return 0;
}