-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnrolledLinkedList.cs
74 lines (64 loc) · 2.03 KB
/
UnrolledLinkedList.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
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.DataStructures.UnrolledLinkedLists
{
public class UnrolledLinkedList<T>
{
private UnrolledLinkedListNode<T> head;
private UnrolledLinkedListNode<T> tail;
public void Add(T value)
{
if (head == null)
{
head = new UnrolledLinkedListNode<T> {Values = {[0] = value}};
head.CurrentIndex += 1;
tail = head;
}
else
{
if (tail.CurrentIndex == 5)
{
tail.Next = new UnrolledLinkedListNode<T> {Values = {[0] = value}};
tail.Next.CurrentIndex += 1;
tail = tail.Next;
}
else
{
tail.Values[tail.CurrentIndex] = value;
tail.CurrentIndex += 1;
}
}
}
public UnrolledLinkedListNode<T> Find(T value)
{
var current = head;
while (current != null)
{
for (var i = 0; i < current.CurrentIndex; i++)
{
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
if (current.Values[i].Equals(value))
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
{
return current;
}
}
current = current.Next;
}
return null;
}
public List<T> Unroll()
{
var current = head;
var result = new List<T>();
while (current != null)
{
for (var i = 0; i < current.CurrentIndex; i++)
{
result.Add(current.Values[i]);
}
current = current.Next;
}
return result;
}
}
}