-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.h
301 lines (279 loc) · 6.04 KB
/
SinglyLinkedList.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
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
#pragma
template <typename Type>
class SinglyLinkedList
{
struct Node
{
Node::Node(Type _value) :value(_value),Next(nullptr){}
Type value;
Node *Next;
};
Node *Start;
Node* FindPreviousNode(const Type & key)
{
Node *temp = Start;
Node *pre = Start;
while (temp != nullptr && temp->value<key)
{
pre = temp;
temp = temp->Next;
}
return pre;
}
Node* FindPreviousOfMatchedNode(const Type & item)
{
Node *handle = Start;
Node *pre=nullptr;
while (handle!=nullptr)
{
if (handle->value == item)
return pre;
pre = handle;
handle = handle->Next;
}
return nullptr;
}
public:
SinglyLinkedList(void);
SinglyLinkedList(const SinglyLinkedList <Type> & obj);
~SinglyLinkedList();
SinglyLinkedList<Type>& operator=(const SinglyLinkedList<Type> & obj);
void InsertAtStart(const Type&item);
void InsertAtEnd(const Type&item);
void Clear();
bool DeleteAtStart();
bool DeleteAtEnd();
void Insert(const Type & item);
Type Peak(bool & IsValid)const;
void Print();
bool Delete(const Type & item);
bool Search(const Type & item);
bool IsEmpty();
};
template <typename Type>
SinglyLinkedList<Type>::SinglyLinkedList(): Start(nullptr){}
template <typename Type>
SinglyLinkedList<Type>::SinglyLinkedList(const SinglyLinkedList <Type> & obj)
{
if (obj.Start == nullptr)
{
this->Start = nullptr;
return;
}
Start = new Node(obj.Start->value);
Node *handle2 = obj.Start->Next;
Node *handle1 = Start;
while (handle2 != nullptr)//until obj have data node
{
handle1->Next = new Node(handle2->value);
// handle1->Next->Previous = handle1;
handle2 = handle2->Next;
handle1 = handle1->Next;
}
return;
}
template <typename Type>
SinglyLinkedList<Type>::~SinglyLinkedList()
{
// cout << "~SinglyLinkedList()\n";
Node *handle = Start;
while (handle)
{
Node *temp = handle->Next;
delete handle;
handle = temp;
}
Start = nullptr;
}
template <typename Type>
SinglyLinkedList<Type>& SinglyLinkedList<Type> ::operator=(const SinglyLinkedList<Type> & obj)
{
if (this != &obj)//obj already same check
{
if (obj.Start == nullptr){
this->Start = nullptr;
return (*this);
}
this->Clear();
Start = new Node(obj.Start->value);
Node *handle2 = obj.Start->Next;
Node *handle1 = Start;
while (handle2 != nullptr)//until obj have data node
{
handle1->Next = new Node(handle2->value);
// handle1->Next->Previous = handle1;
handle2 = handle2->Next;
handle1 = handle1->Next;
}
}
return (*this);
}
template <typename Type>
void SinglyLinkedList<Type>::InsertAtStart(const Type&item)
{
Node *newNode = new Node(item);
newNode->Next = Start;
Start = newNode;
}
template <typename Type>
void SinglyLinkedList<Type>::InsertAtEnd(const Type&item)
{
//note we can have a endNode Pointer in List class but i am doing this
//without that option
//have to ask teacher about this option
//but for now its in this way
if (Start == nullptr)
{
Start = new Node(item);
return;
}
Node *handle = Start;
while (handle->Next != nullptr)
handle = handle->Next;
//handle cantain the last data node
Node *newNode = new Node(item);
//newNode->Next is automatically nullptr here
handle->Next = newNode;
}
template <typename Type>
void SinglyLinkedList<Type>::Clear()
{
Node *handle = Start;
while (handle != nullptr)
{
Node *temp = handle->Next;
delete handle;
handle = temp;
}
// handle = nullptr;
Start = nullptr;
return;
}
template <typename Type>
bool SinglyLinkedList<Type>::IsEmpty()
{
if (Start)
return false;
return true;
}
template <typename Type>
bool SinglyLinkedList<Type>::Search(const Type & item)
{
if (Start == nullptr)
return false;
if (item == Start->value)
return true;
Node *handle = SinglyLinkedList::FindPreviousOfMatchedNode(item);
if (handle!=nullptr)
return true;
//else handle==nullptr=> no match found
return false;
}
template <typename Type>
bool SinglyLinkedList<Type>::Delete(const Type & item)
{
//done
//check for empty list here
if (Start == nullptr)
return false;
else if (item==Start->value)
{
Node *Next = Start->Next;
delete Start;
Start = Next;
}
Node *handle = FindPreviousOfMatchedNode(item);
if (handle == nullptr)//no match flund so no deletion occured
return false;
else{
Node *Next = handle -> Next;
Node *Next_Next = Next->Next;
delete Next;
handle->Next = Next_Next;
}
return true;
}
template <typename Type>
void SinglyLinkedList<Type>::Print()
{
cout << "Element Are: \n";
Node * handle = Start;
while (handle)
{
cout << handle->value << "\n";
handle = handle->Next;
}
}
template <typename Type>
void SinglyLinkedList<Type>::Insert(const Type & item)
{
if (Start != nullptr && Start->value > item)//agr element start pa insert hona ho to.
{
Node *newNode = new Node(item);
newNode->Next = Start;
Start = newNode;
return;
}
Node *handle = SinglyLinkedList::FindPreviousNode(item);
if (handle)
{
Node *newNode = new Node(item);
Node* Next = handle->Next;
handle->Next = newNode;
newNode->Next = Next;
}
else//list empty i.e Find fun return nullptr
{
Start = new Node(item);
Start->Next = nullptr;
}
}
template <typename Type>
bool SinglyLinkedList<Type>::DeleteAtStart()
{
//return bool tels that eiterh element hase deleter or list is empty
if (Start)
{
Node *temp = Start->Next;
delete Start;
Start = temp;
return true;
}
return false;
}
template <typename Type>
bool SinglyLinkedList<Type>::DeleteAtEnd()
{
if (Start == nullptr)
return false;
Node *handle = Start;
Node *pre = nullptr;
while (handle->Next != nullptr)
{
pre = handle;
handle = handle->Next;
}
//pre cantain the previous of last data node
if (pre == nullptr)//only one data node is left in list *check
{
delete Start;
Start = nullptr;
return true;
}
Node *t = pre->Next;
delete t;
pre->Next = nullptr;
return true;
}
template <typename Type>
Type SinglyLinkedList<Type>::Peak(bool & IsValid)const
{
//note its responsibility of user of this liberary to call the IsEmpty() to check eiterh list is empty or not
Type ans;
if (Start != nullptr)
{
IsValid = true;
ans=Start->value;
}else IsValid = false;
return ans;
}