-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListImplementation.cs
301 lines (269 loc) · 8.08 KB
/
LinkedListImplementation.cs
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
using System;
using System.Collections.Generic;
namespace dojo
{
public class LinkedListImplementation
{
public Node HeadNode;
public void Add(int value)
{
if (HeadNode == null)
{
HeadNode = new Node(value);
}
else
{
AddLast(value);
}
}
public void PrintNodeValues()
{
if (HeadNode == null)
{
Console.WriteLine("empty linked list");
}
else if (HeadNode.NextNode == null)
{
Console.WriteLine(HeadNode.Value);
}
else
{
var node = HeadNode;
var values = new List<int>();
while (node.NextNode != null)
{
values.Add(node.Value);
node = node.NextNode;
}
values.Add(node.Value);
Console.WriteLine(string.Join(' ', values));
}
}
public int Size
{
get
{
if (HeadNode == null)
{
return -1;
}
else
{
var node = HeadNode;
var count = 0;
while (node != null)
{
count++;
node = node.NextNode;
}
return count;
}
}
}
public void AddFirst(int value)
{
if (HeadNode == null)
{
HeadNode = new Node(value);
}
else
{
var node = new Node(value, HeadNode);
HeadNode = node;
}
}
public void AddLast(int value)
{
if (HeadNode == null)
{
HeadNode = new Node(value);
return;
}
else
{
var node = HeadNode;
while (node.NextNode != null)
{
node = node.NextNode;
}
node.NextNode = new Node(value);
}
}
public Node DeleteTheFirstElement()
{
var currentHeadNode = HeadNode;
HeadNode = HeadNode.NextNode;
return currentHeadNode;
}
public Node DeleteNode(int value)
{
if (HeadNode == null)
{
throw new Exception("can not delete from an empty list");
}
else
{
if (value == HeadNode.Value)
{
return DeleteTheFirstElement();
}
else
{
var currentNode = HeadNode;
Node nodeToBeRemoved = null;
while (currentNode != null)
{
if (currentNode.NextNode != null
&& currentNode.NextNode.Value == value)
{
nodeToBeRemoved = currentNode.NextNode;
currentNode.NextNode = currentNode.NextNode.NextNode;
break;
}
currentNode = currentNode.NextNode;
}
return nodeToBeRemoved;
}
}
}
public void AddAt(int indexToInsertAt, int valueToInsert)
{
if (indexToInsertAt > Size)
{
AddLast(valueToInsert);
}
else if (indexToInsertAt == 0)
{
AddFirst(valueToInsert);
}
else
{
var currentNodeIndex = 0;
var node = HeadNode;
while (currentNodeIndex < indexToInsertAt - 1)
{
node = node.NextNode;
currentNodeIndex++;
}
var nodeToAdd = new Node(valueToInsert, node.NextNode);
node.NextNode = nodeToAdd;
}
}
public int DeleteAt(int indexToDeleteAt)
{
if (indexToDeleteAt > Size)
{
return -1;
}
else if (indexToDeleteAt == 0)
{
var toReturn = HeadNode;
HeadNode = HeadNode.NextNode;
return toReturn.Value;
}
else
{
var countVar = 0;
var node = HeadNode;
while (countVar < indexToDeleteAt - 1)
{
node = node.NextNode;
countVar++;
}
var nodeToReturn = node.NextNode;
node.NextNode = node.NextNode.NextNode;
return nodeToReturn.Value;
}
}
public Node GetAt(int index)
{
if (Size < index || HeadNode == null)
{
throw new ArgumentException("the list is empty or index out of bounds");
}
else if (index == 0)
{
return HeadNode;
}
else
{
var node = HeadNode;
var counter = 0;
while (counter < index)
{
node = node.NextNode;
counter++;
}
return node;
}
}
public void AddNode(Node node)
{
if (HeadNode == null)
{
HeadNode = node;
}
else
{
var currentNode = HeadNode;
while (currentNode.NextNode != null)
{
currentNode = currentNode.NextNode;
}
currentNode.NextNode = node;
}
}
public static LinkedListImplementation JoinTwoSortedLinkedLists(LinkedListImplementation first,
LinkedListImplementation second)
{
var resultLinkedList = new LinkedListImplementation();
while (first.Size > 0 && second.Size > 0)
{
resultLinkedList.Add(first.GetAt(0).Value < second.GetAt(0).Value ? first.DeleteAt(0) : second.DeleteAt(0));
}
AddElementsFromOneLinkedToAnother(resultLinkedList, first);
AddElementsFromOneLinkedToAnother(resultLinkedList, second);
return resultLinkedList;
}
private static void AddElementsFromOneLinkedToAnother(LinkedListImplementation destination,
LinkedListImplementation source)
{
while (source.Size > 0)
{
destination.AddLast(source.DeleteAt(0));
}
}
public bool IsLinkedListCyclic()
{
if (HeadNode == null)
return false;
var result = false;
var hare = HeadNode;
var tortoise = HeadNode;
while (hare.NextNode.NextNode != null)
{
hare = hare.NextNode.NextNode;
tortoise = tortoise.NextNode;
if (hare != tortoise) continue;
result = true;
break;
}
return result;
}
public static LinkedListImplementation ReverseLinkedList(LinkedListImplementation linkedList)
{
var newLinked = new LinkedListImplementation();
Node previousNode = null;
var currentNode = linkedList.HeadNode;
while (currentNode != null)
{
var nextNode = currentNode.NextNode;
currentNode.NextNode = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
newLinked.HeadNode = previousNode;
return newLinked;
}
}
}