1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export default function LinkedList () {
|
10 | this.head = null
|
11 | this.length = 0
|
12 | }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | LinkedList.prototype.add = function (x) {
|
19 | if (this.head !== null) {
|
20 | this.head.prev = x
|
21 | x.next = this.head
|
22 | }
|
23 | this.head = x
|
24 | ++this.length
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | LinkedList.prototype.remove = function (x) {
|
32 | --this.length
|
33 | if (x === this.head) {
|
34 | this.head = this.head.next
|
35 | }
|
36 | if (x.next !== null) {
|
37 | x.next.prev = x.prev
|
38 | x.next = null
|
39 | }
|
40 | if (x.prev !== null) {
|
41 | x.prev.next = x.next
|
42 | x.prev = null
|
43 | }
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | LinkedList.prototype.isEmpty = function () {
|
50 | return this.length === 0
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | LinkedList.prototype.dispose = function () {
|
59 | if (this.isEmpty()) {
|
60 | return Promise.resolve()
|
61 | }
|
62 |
|
63 | var promises = []
|
64 | var x = this.head
|
65 | this.head = null
|
66 | this.length = 0
|
67 |
|
68 | while (x !== null) {
|
69 | promises.push(x.dispose())
|
70 | x = x.next
|
71 | }
|
72 |
|
73 | return Promise.all(promises)
|
74 | }
|