-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathlinked_list.dart
More file actions
154 lines (115 loc) · 3.15 KB
/
linked_list.dart
File metadata and controls
154 lines (115 loc) · 3.15 KB
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
import 'package:test/test.dart';
class Node<T> {
Node<T?>? next;
T? value;
Node(this.value);
Node.before(this.next, this.value);
}
class LinkedListIterator<T> implements Iterator<T?> {
Node<T?>? _current;
@override
bool moveNext() => _current != null;
@override
T? get current {
T? currentValue = this._current?.value;
this._current = this._current?.next;
return currentValue;
}
LinkedListIterator(this._current);
}
class LinkedList<T> extends Iterable<T?> {
int _length = 0;
int get length => this._length;
Node<T?>? _head;
@override
Iterator<T?> get iterator => new LinkedListIterator<T>(this._head);
void remove(T? item) {
if (this._head?.value == item) {
this._head = this._head?.next;
this._length--;
}
if (this._head != null) {
Node<T?>? current = this._head;
while (current?.next != null) {
if (current?.next?.value == item) {
current?.next = current.next?.next;
this._length--;
}
current = current?.next;
}
}
}
T? pop() {
if (this._head != null) {
T? value = this._head?.value;
this._head = this._head?.next;
this._length--;
return value;
}
return null;
}
void push(T item) {
this._head = new Node.before(this._head, item);
this._length++;
}
void add(T? item) {
if (this._head == null) {
this._head = new Node(item);
} else {
Node<T?>? current = this._head;
while (current?.next != null) {
current = current?.next;
}
current?.next = Node(item);
}
this._length++;
}
}
main() {
test(".add is adding elements in order", () {
LinkedList<double> linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
expect(linkedList, equals([1, 2, 3]));
});
test(".remove is removing all elements with given value", () {
LinkedList<double> linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(2);
linkedList.remove(2);
expect(linkedList, equals([1, 3]));
});
test(".remove on empty list do nothing", () {
LinkedList<double> linkedList = new LinkedList();
linkedList.remove(2);
expect(linkedList, isEmpty);
});
test(".push is appending first element", () {
LinkedList<double> linkedList = new LinkedList();
linkedList.push(1);
expect(linkedList, equals([1]));
linkedList.push(2);
expect(linkedList, equals([2, 1]));
linkedList.push(3);
expect(linkedList, equals([3, 2, 1]));
});
test(".pop is returning and removing first element", () {
LinkedList<double> linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
expect(linkedList.pop(), equals(1));
expect(linkedList, equals([2, 3]));
expect(linkedList.pop(), equals(2));
expect(linkedList, equals([3]));
expect(linkedList.pop(), equals(3));
expect(linkedList, equals([]));
});
test(".pop is returning null when list is empty", () {
LinkedList<double> linkedList = new LinkedList();
expect(linkedList.pop(), isNull);
});
}