-
Notifications
You must be signed in to change notification settings - Fork 292
/
CircularLinkedList.cs
212 lines (172 loc) · 5.08 KB
/
CircularLinkedList.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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Advanced.Algorithms.DataStructures;
/// <summary>
/// A circular linked list implementation.
/// </summary>
public class CircularLinkedList<T> : IEnumerable<T>
{
public CircularLinkedListNode<T> ReferenceNode;
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return new CircularLinkedListEnumerator<T>(ref ReferenceNode);
}
/// <summary>
/// Marks this data as the new reference node after insertion.
/// Like insert first assuming that current reference node as head.
/// Time complexity: O(1).
/// </summary>
public CircularLinkedListNode<T> Insert(T data)
{
var newNode = new CircularLinkedListNode<T>(data);
//if no item exist
if (ReferenceNode == null)
{
//attach the item after reference node
newNode.Next = newNode;
newNode.Previous = newNode;
}
else
{
//attach the item after reference node
newNode.Previous = ReferenceNode;
newNode.Next = ReferenceNode.Next;
ReferenceNode.Next.Previous = newNode;
ReferenceNode.Next = newNode;
}
ReferenceNode = newNode;
return newNode;
}
/// <summary>
/// Time complexity: O(1)
/// </summary>
public void Delete(CircularLinkedListNode<T> current)
{
if (ReferenceNode.Next == ReferenceNode)
{
if (ReferenceNode != current) throw new Exception("Not found");
ReferenceNode = null;
return;
}
current.Previous.Next = current.Next;
current.Next.Previous = current.Previous;
//match is a reference node
if (current == ReferenceNode) ReferenceNode = current.Next;
}
/// <summary>
/// search and delete.
/// Time complexity:O(n).
/// </summary>
public void Delete(T data)
{
if (ReferenceNode == null) throw new Exception("Empty list");
//only one element on list
if (ReferenceNode.Next == ReferenceNode)
{
if (ReferenceNode.Data.Equals(data))
{
ReferenceNode = null;
return;
}
throw new Exception("Not found");
}
//atleast two elements from here
var current = ReferenceNode;
var found = false;
while (true)
{
if (current.Data.Equals(data))
{
current.Previous.Next = current.Next;
current.Next.Previous = current.Previous;
//match is a reference node
if (current == ReferenceNode) ReferenceNode = current.Next;
found = true;
break;
}
//terminate loop if we are about to cycle
if (current.Next == ReferenceNode) break;
current = current.Next;
}
if (found == false) throw new Exception("Not found");
}
/// <summary>
/// Time complexity: O(1).
/// </summary>
public bool IsEmpty()
{
return ReferenceNode == null;
}
/// <summary>
/// Time complexity: O(1).
/// </summary>
public void Clear()
{
if (ReferenceNode == null) throw new Exception("Empty list");
ReferenceNode = null;
}
/// <summary>
/// Time complexity: O(1).
/// </summary>
public void Union(CircularLinkedList<T> newList)
{
ReferenceNode.Previous.Next = newList.ReferenceNode;
ReferenceNode.Previous = newList.ReferenceNode.Previous;
newList.ReferenceNode.Previous.Next = ReferenceNode;
newList.ReferenceNode.Previous = ReferenceNode.Previous;
}
}
/// <summary>
/// Circular linked list node.
/// </summary>
public class CircularLinkedListNode<T>
{
public T Data;
public CircularLinkedListNode<T> Next;
public CircularLinkedListNode<T> Previous;
public CircularLinkedListNode(T data)
{
Data = data;
}
}
internal class CircularLinkedListEnumerator<T> : IEnumerator<T>
{
internal CircularLinkedListNode<T> CurrentNode;
internal CircularLinkedListNode<T> ReferenceNode;
internal CircularLinkedListEnumerator(ref CircularLinkedListNode<T> referenceNode)
{
this.ReferenceNode = referenceNode;
}
public bool MoveNext()
{
if (ReferenceNode == null)
return false;
if (CurrentNode == null)
{
CurrentNode = ReferenceNode;
return true;
}
if (CurrentNode.Next != null && CurrentNode.Next != ReferenceNode)
{
CurrentNode = CurrentNode.Next;
return true;
}
return false;
}
public void Reset()
{
CurrentNode = ReferenceNode;
}
object IEnumerator.Current => Current;
public T Current => CurrentNode.Data;
public void Dispose()
{
ReferenceNode = null;
CurrentNode = null;
}
}