-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDoublyLinkedList.cs
253 lines (192 loc) · 6.25 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
using System;
using System.Collections.Generic;
using System.Runtime;
using System.Text;
namespace DataStructSandBox.DataStructures
{
public interface IDoublyLinkedList<T>{
void Clear();
int Size();
bool IsEmpty();
void Add(T elem);
void AddAtHead(T elem);
void Append(T elem);
T Peek();
T RemoveAt(int idx);
T RemoveLast();
T RemoveFirst();
T PeekLast();
T Remove(T data);
T IndexOf(T data);
bool Contains(T data);
}
class DoublyLinkedList<T> : IDoublyLinkedList
{
int Length = 0;
private Node<T> Head;
private Node<T> Tail;
class Node<T>
{
public T data;
public Node<T> next;
public Node<T> prev;
public Node(T data, Node<T> prev = null, Node<T> next = null)
{
this.data = data;
this.next = next;
this.prev = prev;
}
}
// empty LL O(n)
public void Clear()
{
Node<T> trav = this.Head;
while (trav != null)
{
Node<T> next = trav.next;
trav.prev = trav.next = null;// resetting both pre as well as next pointer to null
trav.data = default(T); // resetting data
trav = next; // move ahead
}
this.Head = this.Tail = trav = null; // reset head, tail and trav
this.Length = 0; // reset size of ll
}
public int Size() => this.Length;
public bool IsEmpty() => this.Length == 0;
public void Add(T elem) => Append(elem);
public void AddAtHead(T elem)
{
if (this.IsEmpty())
this.Head = this.Tail = new Node<T>(elem);
else
{
this.Head.prev = new Node<T>(elem);
this.Head = this.Head.prev;
}
this.Length++;
}
public void Append(T elem)
{
if (this.IsEmpty())
this.Head = this.Tail = new Node<T>(elem);
else
{
this.Tail.next = new Node<T>(elem);
this.Tail = this.Tail.next;
}
this.Length++;
}
// check val of first node if non empty
public T Peek()
{
if (this.IsEmpty())
throw new NullReferenceException();
return this.Head.data;
}
public T PeekLast()
{
if (this.IsEmpty())
throw new NullReferenceException();
return this.Tail.data;
}
public T RemoveFirst()
{
if (this.IsEmpty())
throw new NullReferenceException();
T data = this.Head.data;
this.Head = Head.next;
this.Length--;
if (this.IsEmpty()) this.Tail = null;// if list is empty then set tail to null as well
else this.Head.prev = null; // free memory for node which is now deleted
return data;
}
public T RemoveLast()
{
if (this.IsEmpty())
throw new NullReferenceException();
T data = this.Tail.data;
this.Tail = Tail.prev;
this.Length--;
if (this.IsEmpty()) this.Head = null;// if list is empty then set head to null as well
else this.Tail.next = null; // free memory for node which is now deleted
return data;
}
private T RemoveNode(Node<T> node)
{
// If the node is the head node
if (node.prev == null) return this.RemoveFirst();
// If the node is tail
if (node.next == null) return this.RemoveLast();
// skip this node/ adjust pointers
node.prev.next = node.next;
node.next.prev = node.prev;
T data = node.data;// save data to return
node.data = default(T);// reset data
node = node.prev = node.next = null; // reset pointers
this.Length--;
return data;
}
public T RemoveAt(int idx)
{
if (idx < 0 || idx >= this.Length) throw new IndexOutOfRangeException();
Node<T> trav;
if(idx < this.Length / 2)
{
trav = this.Head;
for (int i = 0; i != idx; i++)
trav = trav.next;
}
else
{
trav = this.Tail;
for (int i = this.Length-1; i != idx; i--)
trav = trav.prev;
}
return this.RemoveNode(trav);
}
public T Remove(T data)
{
throw new NotImplementedException();
}
public T IndexOf(T data)
{
throw new NotImplementedException();
}
public bool Contains(T data)
{
throw new NotImplementedException();
}
public override string ToString()
{
if (this.IsEmpty())
return "[]";
StringBuilder sb = new StringBuilder();
sb.Append("[ ");
Node<T> trav = this.Head;
while (trav.next != null)
{
sb.AppendFormat("{0}, ", trav.data);
trav = trav.next;
}
sb.AppendFormat("{0} ]", trav.data);
return sb.ToString();
}
}
// driver code
////public class Program
////{
//// public static int Main()
//// {
//// DoublyLinkedList<int> DLL = new DoublyLinkedList<int>();
//// DLL.Add(1);
//// DLL.Add(5);
//// DLL.Add(57);
//// DLL.Add(9);
//// DLL.Add(23);
//// DLL.Add(74);
//// Console.WriteLine(DLL.ToString());
//// // output: [ 1, 5, 57, 9, 23, 74 ]
//// return 0;
//// }
////}
}