1
- import Node from './Node.js' ;
1
+ import {
2
+ Node ,
3
+ _concat ,
4
+ _erase ,
5
+ _extend ,
6
+ _remove ,
7
+ _insertAfter ,
8
+ _insertBefore ,
9
+ } from '@data-structure-algebra/doubly-linked-list' ;
10
+
2
11
import Iterator from './Iterator.js' ;
3
12
import ReverseIterator from './ReverseIterator.js' ;
4
13
@@ -9,17 +18,15 @@ import ReverseIterator from './ReverseIterator.js';
9
18
*/
10
19
export default function DoublyLinkedList ( ) {
11
20
this . front = new Node ( null , null , null ) ;
12
- this . back = new Node ( this . front , null , null ) ;
21
+ this . back = new Node ( null , this . front , null ) ;
13
22
this . front . next = this . back ;
14
23
this . length = 0 ;
15
24
}
16
25
17
26
DoublyLinkedList . prototype . insertAfter = function ( iterator , value ) {
18
27
const prev = iterator . current ;
19
28
20
- const node = new Node ( prev , prev . next , value ) ;
21
- prev . next . prev = node ;
22
- prev . next = node ;
29
+ const node = _insertAfter ( prev , value ) ;
23
30
24
31
++ this . length ;
25
32
return this . iterator ( node ) ;
@@ -28,9 +35,7 @@ DoublyLinkedList.prototype.insertAfter = function (iterator, value) {
28
35
DoublyLinkedList . prototype . insertBefore = function ( iterator , value ) {
29
36
const next = iterator . current ;
30
37
31
- const node = new Node ( next . prev , next , value ) ;
32
- next . prev . next = node ;
33
- next . prev = node ;
38
+ const node = _insertBefore ( next , value ) ;
34
39
35
40
++ this . length ;
36
41
return this . iterator ( node ) ;
@@ -47,8 +52,7 @@ DoublyLinkedList.prototype.push = function (value) {
47
52
DoublyLinkedList . prototype . erase = function ( iterator ) {
48
53
const node = iterator . current ;
49
54
50
- node . prev . next = node . next ;
51
- node . next . prev = node . prev ;
55
+ _remove ( node ) ;
52
56
53
57
-- this . length ;
54
58
return this . iterator ( node . next ) ;
@@ -57,8 +61,7 @@ DoublyLinkedList.prototype.erase = function (iterator) {
57
61
DoublyLinkedList . prototype . rerase = function ( iterator ) {
58
62
const node = iterator . current ;
59
63
60
- node . next . prev = node . prev ;
61
- node . prev . next = node . next ;
64
+ _remove ( node ) ;
62
65
63
66
-- this . length ;
64
67
return this . iterator ( node . prev ) ;
@@ -68,14 +71,13 @@ DoublyLinkedList.prototype.eraserange = function (first, last) {
68
71
const firstnode = first . current ;
69
72
const lastnode = last . current ;
70
73
71
- lastnode . prev = firstnode . prev ;
72
- firstnode . prev . next = lastnode ;
74
+ _erase ( firstnode , lastnode ) ;
73
75
74
- const it = first . copy ( ) ;
76
+ let x = first . current ;
75
77
76
- while ( it . current !== lastnode ) {
78
+ while ( x !== lastnode ) {
77
79
-- this . length ;
78
- it . next ( ) ;
80
+ x = x . next ;
79
81
}
80
82
81
83
return last . copy ( ) ;
@@ -85,14 +87,13 @@ DoublyLinkedList.prototype.reraserange = function (first, last) {
85
87
const firstnode = first . current ;
86
88
const lastnode = last . current ;
87
89
88
- lastnode . next = firstnode . next ;
89
- firstnode . next . prev = lastnode ;
90
+ _concat ( lastnode , firstnode . next ) ;
90
91
91
- const it = first . copy ( ) ;
92
+ let x = first . current ;
92
93
93
- while ( it . current !== lastnode ) {
94
+ while ( x !== lastnode ) {
94
95
-- this . length ;
95
- it . next ( ) ;
96
+ x = x . prev ;
96
97
}
97
98
98
99
return last . copy ( ) ;
@@ -102,9 +103,7 @@ DoublyLinkedList.prototype.shift = function () {
102
103
if ( this . length === 0 ) return null ;
103
104
104
105
const node = this . front . next ;
105
-
106
- this . front . next = node . next ;
107
- node . next . prev = this . front ;
106
+ _remove ( node ) ;
108
107
109
108
-- this . length ;
110
109
@@ -115,18 +114,15 @@ DoublyLinkedList.prototype.pop = function () {
115
114
if ( this . length === 0 ) return null ;
116
115
117
116
const node = this . back . prev ;
118
-
119
- this . back . prev = node . prev ;
120
- node . prev . next = this . back ;
117
+ _remove ( node ) ;
121
118
122
119
-- this . length ;
123
120
124
121
return node . value ;
125
122
} ;
126
123
127
124
DoublyLinkedList . prototype . clear = function ( ) {
128
- this . front . next = this . back ;
129
- this . back . prev = this . front ;
125
+ _concat ( this . front , this . back ) ;
130
126
this . length = 0 ;
131
127
return this ;
132
128
} ;
@@ -157,7 +153,8 @@ DoublyLinkedList.prototype.rend = function () {
157
153
158
154
const from = ( iterable ) => {
159
155
const list = new DoublyLinkedList ( ) ;
160
- for ( const value of iterable ) list . push ( value ) ;
156
+ const last = _extend ( list . front , iterable ) ;
157
+ _concat ( last , list . back ) ;
161
158
return list ;
162
159
} ;
163
160
0 commit comments