-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.cs
222 lines (186 loc) · 5.39 KB
/
Deque.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
using System.Text;
namespace propcalc;
public class Deque<T> {
public class Node {
public T data;
public Node prev, next;
public Node() {
data = default!; prev = default!; next = default!;
}
public Node(T d, Node p, Node n) {
data = d; prev = p; next = n;
}
}
public readonly DequeIterator iterator;
private readonly Node head, tail;
private int size;
public Deque() {
head = new Node();
tail = new Node();
head.next = tail;
head.prev = head;
tail.prev = head;
tail.next = tail;
iterator = new DequeIterator(this);
}
public Deque(IEnumerable<T> init) {
head = new Node();
tail = new Node();
head.next = tail;
head.prev = head;
tail.prev = head;
tail.next = tail;
iterator = new DequeIterator(this);
foreach (var i in init)
Append(i);
}
private Node First()
=> head.next;
private Node Last()
=> tail.prev;
public int Size()
=> size;
public T ReadFirst()
=> First().data;
public T ReadLast()
=> Last().data;
public void SetLast(T data)
=> Last().data = data;
public void SetFirst(T data)
=> First().data = data;
public T this[int index] {
get => iterator.To(index);
set {
iterator.To(index);
iterator.Set(value);
}
}
public void Append(T data) {
var n = new Node(data, Last(), tail);
tail.prev.next = n;
tail.prev = n;
++size;
}
public void Prepend(T data) {
var n = new Node(data, head, First());
head.next.prev = n;
head.next = n;
++size;
}
public T PopFirst() {
var t = ReadFirst();
head.next = First().next;
First().next.prev = head;
--size;
return t;
}
public T PopLast() {
var t = ReadLast();
tail.prev = Last().prev;
Last().prev.next = tail;
--size;
return t;
}
private void Remove(Node n) {
n.next.prev = n.prev;
n.prev.next = n.next;
--size;
}
public void Clear() {
head.next = tail;
tail.prev = head;
size = 0;
}
public void RemoveDuplicates(T data) {
var n = First();
bool found = false;
while (n != tail) {
if (found && n.data!.Equals(data))
Remove(n);
if (n.data!.Equals(data))
found = true;
n = n.next;
}
}
public Deque<T> ConvertToSet() {
var i = GetIterator();
while (i.ToNext())
RemoveDuplicates(i.Get());
return this;
}
public T[] ToArray() {
var n = First();
T[] arr = new T[size];
for (int i = 0; i < size; n = n.next)
arr[i++] = n.data;
return arr;
}
public override string ToString() {
StringBuilder s = new();
var n = First();
while (n != tail) {
s.Append(n.data + " ");
n = n.next;
}
return s.ToString();
}
public DequeIterator GetIterator() {
return new DequeIterator(this);
}
public class DequeIterator {
private Node curr;
private readonly Deque<T> deque;
private int index = -1;
public DequeIterator(Deque<T> deq) {
curr = deq.head;
deque = deq;
}
public void Reset() => curr = deque.head;
public bool HasNext() => curr != deque.Last();
public bool HasPrev() => curr != deque.First();
public T GetNext() => curr.next.data;
public T GetPrev() => curr.prev.data;
public T Get() => curr.data;
public void Set(T data) => curr.data = data;
public T To(int i) {
if (i == 0) {
index = 0;
curr = deque.First();
return Get();
}
if (i > index)
while (index < i)
ToNext();
else if (i < index)
while (index > i)
ToPrev();
return Get();
}
public bool ToNext() {
curr = curr.next;
if (curr == deque.tail) index = deque.Size();
else ++index;
return curr != deque.tail;
}
public bool ToPrev() {
curr = curr.prev;
if (curr == deque.head) index = -1;
else --index;
return curr != deque.head;
}
public bool RemoveNext() {
if (curr == deque.tail.prev || curr.next == deque.tail)
return false;
curr.next.next.prev = curr;
curr.next = curr.next.next;
--deque.size;
return true;
}
public void AddNext(T data) {
Node n = new(data, curr, curr.next);
curr.next.prev = n;
curr.next = n;
++deque.size;
}
}
}