-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathDLL.cs
97 lines (86 loc) · 2.57 KB
/
DLL.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
public class DLL<T> : System.Collections.Generic.IEnumerable<T>
{
private ListNode<T> _head;
private ListNode<T> _tail;
public int Count { get; private set; }
/// <summary>
/// Adding new node to tail of list
/// </summary>
/// <param name="value">Value for new node</param>
public void Add(T value)
{
ListNode<T> _tmpNode = new ListNode<T>(value);
if (_head == null)
{
_head = _tmpNode;
_tail = _tmpNode;
}
else
{
_tail.Next = _tmpNode;
_tail.Next.Previous = _tail;
_tail = _tmpNode;
}
Count++;
}
/// <summary>
/// Make your list clear
/// </summary>
public void Clear()
{
_head = null;
_tail = null;
Count = 0;
}
/// <summary>
/// Removing node by value from list
/// </summary>
/// <param name="value">Value of node</param>
/// <returns></returns>
public bool Remove(T value)
{
ListNode<T> previous = null;
ListNode<T> current = _head;
while (current != null)
{
if (current.Value.Equals(value))
{
if (previous != null)
{
previous.Next = current.Next;
current.Next.Previous = current.Previous;
if (current.Next == null)
{
_tail = previous;
}
}
else
{
_head = _head.Next;
if (_head == null)
{
_tail = null;
}
}
Count--;
return true;
}
previous = current;
current = current.Next;
}
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
ListNode<T> current = _head;
while (current != null)
{
yield return current.Value;
current = current.Next;
}
}
}