-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab5_ArrayBasedList.h
More file actions
386 lines (298 loc) · 7.2 KB
/
Lab5_ArrayBasedList.h
File metadata and controls
386 lines (298 loc) · 7.2 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//Design an application for List class using templates, with following data members :
//a. A pointer of type T
//b. A variable capacity of type int to hold the total capacity of the list
//c. A variable counter of type int which holds the current index of the list
//Your class should be able to perform following operations :
//
//1 insert(T item)
//Inserts an itemand increments the counter only if the list is not full
//
//2 insertAt(T item, int index)
//The function takes two parameters i.e., item of type Tand an integer parameter which points
//to the position where the insertion is to be done.Make sure you do not overwrite values.For
//this you will have to increment the counter to create additional space for the item to be
//inserted.
//
//3 insertAfter(T itemTobeInserted, T item)
//The first parameter of this function represents the item to be inserted in the list, the second
//parameter represents the item already present in the list.You must search the item already
//present in the listand then insert the itemtobeinserted after item.Use the functions of your
//own code instead of replicating logic.
//
//4 insertBefore(T itemTobeInserted, int item)
//The first parameter of this function represents the item to be inserted in the list, the second
//parameter represents the item already present in the list.You must search the item already
//present in the listand then insert the itemtobeinserted before item.Use the functions of your
//own code instead of replicating logic, i.e.
//
//5 isEmpty()
//Checks if the counter variable is zero, i.e., the list is empty there are no elements.
//
//6 isFull()
//Checks if the counter variable has reached the total capacity of the List, which means no
//more insertions are possible.
//
//7 remove(T item)
//Removes item passed through parameters.Make sure you update the counter after removing
//one item.
//
//8 removeBefore(T item)
//You pass an item through parameter, then search for it in the array, then remove the item
//present before this item.i.e. if I have{ 1, 2, 3, 4 } in array.I pass 3 to this function the
//resultant array should look like{ 1,3,4 }. Make sure you update counter after removing one
//item.
//
//9 removeAfter(T item)
//You pass an item through parameter, then search for it in the array, then remove the item
//present after this item.i.e. if I have{ 1, 2, 3, 4, 5 } in array.I pass 3 to this function the
//resultant array should look like{ 1,2, 3, 5 }. Make sure you update counter after removing one
//item.
//
//10 search(T item)
//This function searches for the item passed through parameter.If item exists, return index
//number.But if you didn’t get the item in the list, what are you going to return ?
//
//11 print()
//Print all the items of the List.
//
//12 operator==(List & amp; L)
//we can overload == to compare the items of two lists.If the lengths of both the lists aren’t
//same, you don’t have to go further to check for the items, i.e., the lists are already unequal.
//
//13 reverse()
//Reverse all the elements of the List.
#pragma once
#include<iostream>
using namespace std;
template <typename T>
class List
{ public:
T* values;
int capacity;
int counter;
List(int cap)
{
capacity = cap;
counter = 0;
values = new T[capacity];
}
bool isFull()
{
if (capacity == counter)
return true;
else
return false;
}
bool isEmpty()
{
if (counter==0)
return true;
else
return false;
}
bool insert(T val)
{
if (!isFull())
{
values[counter] = val;
counter++;
return true;
}
else
return false;
}
bool insertAt(T val, int index)
{
if (!isFull())
{
int sizeprev = counter;
counter++;
T* temp = new T[counter];
for (int i = 0; i < sizeprev+1; i++)
{
if(i>index)
temp[i] = values[i-1];
if (i < index)
temp[i] = values[i];
}
for (int i = 0; i < counter; i++)
{
if (i == index)
temp[index] = val;
}
values = temp;
return true;
}
else
return false;
}
bool insertAfter(T val, T item)
{
if (!isFull())
{
int itemPos;
int sizeprev = counter;
for (int i = 0; i < counter; i++)
{
if (values[i] == item)
itemPos = i+1;
}
counter++;
T* temp = new T[counter];
for (int i = 0; i < sizeprev+1; i++)
{
if(i>itemPos)
temp[i] = values[i-1];
if (i < itemPos)
temp[i] = values[i];
}
for (int i = 0; i < counter; i++)
{
if (i == itemPos)
temp[itemPos] = val;
}
values = temp;
return true;
}
else
return false;
}
bool insertBefore(T val, int item)
{
if (!isFull())
{
int itemPos;
int sizeprev = counter;
for (int i = 0; i < counter; i++)
{
if (values[i] == item)
itemPos = i ;
}
counter++;
T* temp = new T[counter];
for (int i = 0; i < sizeprev + 1; i++)
{
if (i > itemPos)
temp[i] = values[i - 1];
if (i < itemPos)
temp[i] = values[i];
}
for (int i = 0; i < counter; i++)
{
if (i == itemPos)
temp[itemPos] = val;
}
values = temp;
return true;
}
else
return false;
}
bool search(T val)
{
bool found = false;
for (int i = 0; i < capacity; i++)
{
if (values[i] == val)
{
found = true;
}
}
return found;
}
bool remove(T val)
{
bool flag = false;
int itemPos;
int sizeprev = counter;
for (int i = 0; i < counter; i++)
{
if (values[i] == val)
itemPos = i;
}
counter--;
T* temp = new T[counter];
for (int i = 0; i < sizeprev; i++)
{
if (i > itemPos)
temp[i - 1] = values[i];
if (i < itemPos)
temp[i] = values[i];
}
values = temp;
return true;
}
bool removeBefore(T val)
{
bool flag = false;
int itemPos;
int sizeprev = counter;
for (int i = 0; i < counter; i++)
{
if (values[i] == val)
itemPos = i-1;
}
counter--;
T* temp = new T[counter];
for (int i = 0; i < sizeprev; i++)
{
if (i > itemPos)
temp[i - 1] = values[i];
if (i < itemPos)
temp[i] = values[i];
}
values = temp;
return true;
}
bool removeAfter(T val)
{
bool flag = false;
int itemPos;
int sizeprev = counter;
for (int i = 0; i < counter; i++)
{
if (values[i] == val)
itemPos = i + 1;
}
counter--;
T* temp = new T[counter];
for (int i = 0; i < sizeprev; i++)
{
if (i > itemPos)
temp[i - 1] = values[i];
if (i < itemPos)
temp[i] = values[i];
}
values = temp;
return true;
}
bool operator==(List& L)
{
bool flag = true;
if (counter != L.counter)
flag = false;
else
for (int i = 0; i < counter; i++)
{
if (values[i] != L.values[i])
flag = false;
}
return flag;
}
void reverse()
{
T* temp = new T[counter];
for (int i = 0; i < counter; i++)
{
temp[counter-1-i] = values[i];
}
values = temp;
}
void print()
{
for (int i = 0; i < counter; i++)
{
cout << values[i] << " ";
}
}
};