-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cdbll.cpp
327 lines (296 loc) · 6.57 KB
/
Cdbll.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
320
321
322
323
324
325
326
#include"Cdbll.h"
//constructor
template<class T>
Cdbll<T> ::Cdbll()
{
first = NULL;
last = NULL;
count = 0;
}
//function to destroy all the node
template <class T>
void Cdbll<T>::destroy()
{
Node <T>* temp;
if (first == NULL)
{
return;
}
else
{
do
{
temp = first;
last = first->next;
first = first->next;
delete temp;
count--;
} while (last != first);
}
first =NULL;// set first ==NULL
last = NULL;// last == NULL
count = 0;
}
//destructor
template<class T>
Cdbll<T> :: ~Cdbll()
{
destroy();
}
//function to insert at the front
template< class T>
void Cdbll<T>::insertFront(const T& item)
{
if (first == NULL)
{
Node<T>* newNode;
newNode = new Node<T>;// allocate memory
newNode->value = item;//insert value
first = last = newNode;
newNode->prev = last;
newNode->next = first;
}
else
{
Node<T>* newNode1;
newNode1 = new Node<T>;
newNode1->value = item;
newNode1->next = first;
first->prev = newNode1;
newNode1->prev = last;
last->next = newNode1;
first = newNode1;
}
count++;
}
// function to insert at the Back
template <class T>
void Cdbll<T>::insertEnd(const T& item)
{
if (first == NULL)
{
Node<T>* newNode;
newNode = new Node<T>;
newNode->value = item;
first = last = newNode;
newNode->prev = last;
newNode->next = first;
}
else
{
Node<T>* newNode1;
newNode1 = new Node<T>;
newNode1->value = item;
newNode1->prev = last;
last->next = newNode1;
newNode1->next = first;
first->prev = newNode1;
last = newNode1;
}
count++;
}
//print function
template< class T>
void Cdbll<T>::print()
{
Node<T>* current;
current = first;
if (first == NULL)
{
cout << " The list is empty" << endl;
}
else
{ cout<<"copy: ";
do
{
cout << current->value << " ";
current = current->next;
} while (current!=first );
}
}
//copylist function for operator = equal and copy constructor
template<class T>
void Cdbll<T> ::copylist(const Cdbll<T>& otherlist)
{
int count1 ;
count1 = counterlist();
if (first != NULL) destroy(); //if the list is nonempty, make it empty
if (otherlist.first == NULL) //otherlist is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
Node<T>* current;
Node<T>* currentother;
first = new Node<T>;
first->value = otherlist.first->value;
current = first;
currentother = otherlist.first;
currentother = currentother->next;
// cout<<"first copy"<<first->value<<endl;
while (currentother != otherlist.first)
{
current->next = new Node<T>;
current->next->value = currentother->value;
current->next->prev = current;
current = current->next;
currentother = currentother->next;
//cout<<"copied"<<current->value<<endl;
}
last = current;
last->next = first;
first->prev = last;
count = otherlist.count;
}//end else
}
// Count function
template<class T>
int Cdbll<T> ::counterlist()
{
return count;//return the number of element inside the doubly circular linked list
}
//client side count function
template<class T>
int Cdbll<T> ::length()
{
int a = counterlist(); //function that the client call access to knwow the length of his list
return a;
}
//copy constructor
template<class T>
Cdbll<T> ::Cdbll(const Cdbll<T>& otherlist)
{
copylist(otherlist);
}
//operator= overloading
template<class T>
void Cdbll<T> :: operator=(const Cdbll<T> & otherlist)
{
// destroy();
copylist(otherlist);
}
// function to delete Node
template<class T>
void Cdbll<T> ::deleteItem(const T& item)
{
int l= counterlist();//variable to stop the loop if the value is not in the list
Node<T>* p;
Node<T>* temp;
Node<T>* q;
if (first == NULL)
return;
p = new Node <T>;
p = first;
q = first;
while (p->value != item && l!=0)
{
if (p->next == first)
{
cout << " There is no node with value " << item << " to delete" << endl;
}
q = p;
p = p->next;
l--;
}// end loop
if (p->next == first && q == NULL) // check only if one node is available
{
first = NULL;
delete p;
count--;
}
if (p == first) //check if delete is assign to the first node
{
p = first;
first = first->next;
first->prev = last;
last->next = first;
delete p;
}
else if (p->next ==first) // check if delete is assign to the last node
{
q->next = first;
first->prev = q;
last = q;
delete p;
}
else // check if delete is assign to position s different from the one ellicted above
{
q = first;
temp = p->next;
q->next = temp;
temp->prev = q;
delete p;
}
count--;
}
//empty the list;
template<class T>
void Cdbll<T> ::emptylist()
{
destroy();
}
//function to search for item
template<class T>
bool Cdbll<T> ::searchItem( T item)
{
bool found = false;
Node<T>* current;
current = first;
if (current == NULL)
{
return false;
}
else
{
do
{
if (current->value == item)
found = true;
else
current = current->next;
} while (!found && current != first);
return found;
}
}
//function to check if the list is NULL
template<class T>
bool Cdbll<T> ::isEmpty()
{
return(first == NULL);
}
//iterate functions
template<class T>
void Cdbll<T> ::iterateFront()
{
Node<T>* current;
current = first;
do
{
if (current == NULL)
cout << "empty list: I cannot Iterate" << endl;
else
{
cout << current->value << " ";
current = current->next;
}
} while (current != first);
}
template<class T>
void Cdbll<T> ::iterateBack()
{
Node<T>* current;
current = last;
do
{
if (current == NULL)
cout << "empty list: I cannot Iterate" << endl;
else
{
cout << current->value << " ";
current = current->prev;
}
} while (current != last);
}