-
Notifications
You must be signed in to change notification settings - Fork 11
/
DoublyLinkedList.cs
261 lines (232 loc) · 6.76 KB
/
DoublyLinkedList.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
DoublyLinkedList linkedList = new();
linkedList.SetHead(new Node(1));
linkedList.SetTail(new Node(5));
linkedList.PrintLinkedList();
Console.WriteLine();
Console.WriteLine("New Head: 0");
linkedList.SetHead(new Node(0));
linkedList.PrintLinkedList();
Console.WriteLine();
Console.WriteLine("Add 3, Before : 5");
Node node5 = linkedList.GetNodeWithValue(5);
linkedList.InsertBefore(node5, new Node(3));
linkedList.PrintLinkedList();
Console.WriteLine();
Console.WriteLine("Add 4, After : 3");
Node node3 = linkedList.GetNodeWithValue(3);
linkedList.InsertAfter(node3, new Node(4));
linkedList.PrintLinkedList();
Console.WriteLine();
Console.WriteLine("Insert 2 at position 3");
linkedList.InsertAtPosition(3, new Node(2));
linkedList.PrintLinkedList();
Console.WriteLine();
Console.WriteLine("Contains 4?: " + linkedList.ContainsNodeWithValue(4));
Console.WriteLine();
Console.WriteLine("Removing node with value: 4");
linkedList.RemoveNodesWithValue(4);
linkedList.PrintLinkedList();
Console.WriteLine();
Console.WriteLine("Removing head! 0");
linkedList.Remove(linkedList.Head);
linkedList.PrintLinkedList();
Console.WriteLine();
public class DoublyLinkedList
{
public Node Head;
public Node Tail;
//Time: O(1), Space: O(1)
public void SetHead(Node node)
{
if (this.Head == null)
{
this.Head = node;
this.Tail = node;
}
else
{
InsertBefore(Head, node);
}
}
//Time: O(1), Space: O(1)
public void SetTail(Node node)
{
//if list is empty.
if (this.Tail == null)
{
//Since head creates a node which is both head and tail.
SetHead(node);
}
else
{
InsertAfter(Tail, node);
}
}
//Time: O(1), Space: O(1)
public void InsertBefore(Node node, Node nodeToInsert)
{
//Check if there is only one node in list
if (nodeToInsert == Head && nodeToInsert == Tail)
{
return;
}
Remove(nodeToInsert);
// Insert before 40, 30: 10 <-> 20 <-> 40
// First make sure new Node is in place
nodeToInsert.Next = node; // 30 -> 40 //If node was first node then newNode will point to null
nodeToInsert.Prev = node.Prev; // 20 <- 30
//If node was first node then newNode will point to null but we have make it Head
if (node.Prev == null)
{
this.Head = nodeToInsert;
}
else
{
//if it is not a first node then node's previous's next will point to newNode
node.Prev.Next = nodeToInsert; // 20 -> 30
}
node.Prev = nodeToInsert; //30 <- 40
}
//Time: O(1), Space: O(1)
public void InsertAfter(Node node, Node nodeToInsert)
{
//Check if there is only one node in list
if (nodeToInsert == Head && nodeToInsert == Tail)
{
return;
}
Remove(nodeToInsert);
//Insert after 20, 30: 10 <-> 20 <-> 40
//First make sure new Node is in place
nodeToInsert.Next = node.Next; // 30 -> 40 //If node was last node then newNode will point to null
nodeToInsert.Prev = node; // 20 <- 30
//If node was last node then newNode will point to null but we have make it Tail
if (node.Next == null)
{
this.Tail = nodeToInsert;
}
else
{
//if it is not a last node then node's next's previous will point to newNode
node.Next.Prev = nodeToInsert; // 30 <- 40
}
node.Next = nodeToInsert; //20 -> 30
}
//Time: O(position), Space: O(1)
public void InsertAtPosition(int position, Node nodeToInsert)
{
if (position == 1)
{
SetHead(nodeToInsert);
}
else
{
// Insert at 3, 30: 10 <-> 20 <-> 40
int currentPosition = 1;
Node currentNode = this.Head;
while (currentNode != null && position != currentPosition)
{
currentPosition++;
currentNode = currentNode.Next;
}
if (currentNode != null)
{
InsertBefore(currentNode, nodeToInsert);
}
else
{
//Reached last element// position : 4
SetTail(nodeToInsert);
}
}
}
//Time: O(1), Space: O(1)
public void Remove(Node node)
{
// Remove Head 10 <-> 20 <-> 30 <-> 40
if (node == this.Head)
{
this.Head = this.Head.Next; //20 <-> 30 <-> 40
}
// Remove Tail 40 <-> 20 <-> 30 <-> 40
if (node == this.Tail)
{
this.Tail = this.Tail.Prev; //10 <-> 20 <-> 30
}
RemoveNodeFromBetween(node);
}
//Time: O(n), Space: O(1)
public void RemoveNodesWithValue(int value)
{
// Remove 30: 10 <-> 20 <-> 30 <-> 40
//Traverse the list
Node currentNode = this.Head;
while (currentNode != null)
{
Node nodeToRemove = currentNode;
currentNode = currentNode.Next;
if (nodeToRemove.Value == value)
{
Remove(nodeToRemove);
}
}
}
//Time: O(n), Space: O(1)
private void RemoveNodeFromBetween(Node node)
{
// Remove 20: 10 <-> 20 <-> 30 <-> 40
if (node.Prev != null)
{
node.Prev.Next = node.Next; // 10 -> 30
}
if (node.Next != null)
{
node.Next.Prev = node.Prev; // 10 <- 30
}
//Removing references
node.Prev = null;
node.Next = null;
}
//Time: O(n), Space: O(1)
public bool ContainsNodeWithValue(int value)
{
// 30: 10 <-> 20 <-> 30 <-> 40
//Traverse the list
Node currentNode = this.Head;
while (currentNode != null && currentNode.Value != value)
{
currentNode = currentNode.Next;
}
return currentNode != null;
}
//Time: O(n), Space: O(1)
public Node GetNodeWithValue(int value)
{
// 30: 10 <-> 20 <-> 30 <-> 40
//Traverse the list
Node currentNode = this.Head;
while (currentNode != null && currentNode.Value != value)
{
currentNode = currentNode.Next;
}
return currentNode;
}
//Time: O(n), Space: O(1)
public void PrintLinkedList()
{
Node head = this.Head;
while (head != null)
{
Console.Write(head.Value + " <-> ");
head = head.Next;
}
Console.WriteLine("Null");
}
}
public class Node
{
public int Value;
public Node Prev;
public Node Next;
public Node(int value) => this.Value = value;
}