@@ -6,102 +6,141 @@ function Node(data) {
6
6
7
7
class DoublyLinkedList {
8
8
constructor ( ) {
9
- this . head = null ;
10
- this . tail = null ;
9
+ const dummyNode = new Node ( undefined ) ;
10
+ dummyNode . next = dummyNode ;
11
+ dummyNode . previous = dummyNode ;
12
+ this . dummy = dummyNode ;
11
13
this . numberOfValues = 0 ;
12
14
}
13
15
14
- add ( data ) {
15
- const node = new Node ( data ) ;
16
- if ( ! this . head ) {
17
- this . head = node ;
18
- this . tail = node ;
19
- } else {
20
- node . previous = this . tail ;
21
- this . tail . next = node ;
22
- this . tail = node ;
16
+ /* this should be a private method */
17
+ getNode ( i ) {
18
+ let node = undefined ;
19
+ if ( i < Math . floor ( this . numberOfValues / 2 ) ) {
20
+ node = this . dummy . next ;
21
+ for ( let j = 0 ; j < i ; j ++ ) {
22
+ node = node . next ;
23
+ }
24
+ } else {
25
+ node = this . dummy ;
26
+ for ( let j = this . numberOfValues ; j > i ; j -- ) {
27
+ node = node . previous ;
28
+ }
23
29
}
30
+ return node ;
31
+ }
32
+
33
+ /* gets a values within the range of 0 > i >= n */
34
+ get ( i ) {
35
+ if ( 0 > i || i >= this . numberOfValues ) return undefined ;
36
+ return this . getNode ( i ) . data ;
37
+ }
38
+
39
+ /* sets a value withing the range 0 > i >= n */
40
+ set ( i , x ) {
41
+ if ( 0 > i || i >= this . numberOfValues ) return undefined ;
42
+ let node = getNode ( i ) ;
43
+ let temp = node . data ;
44
+ node . data = x ;
45
+ return temp ;
46
+ }
47
+
48
+ /* this should be a private method*/
49
+ addBefore ( node , x ) {
50
+ const toAdd = new Node ( x ) ;
51
+
52
+ toAdd . previous = node . previous ;
53
+ toAdd . next = node ;
54
+
55
+
56
+ toAdd . next . previous = toAdd ;
57
+ toAdd . previous . next = toAdd ;
58
+
24
59
this . numberOfValues ++ ;
60
+
61
+ return toAdd ;
25
62
}
26
63
27
- remove ( data ) {
28
- let current = this . head ;
29
- while ( current ) {
30
- if ( current . data === data ) {
31
- if ( current === this . head && current === this . tail ) {
32
- this . head = null ;
33
- this . tail = null ;
34
- } else if ( current === this . head ) {
35
- this . head = this . head . next ;
36
- this . head . previous = null ;
37
- } else if ( current === this . tail ) {
38
- this . tail = this . tail . previous ;
39
- this . tail . next = null ;
40
- } else {
41
- current . previous . next = current . next ;
42
- current . next . previous = current . previous ;
43
- }
44
- this . numberOfValues -- ;
45
- }
46
- current = current . next ;
47
- }
64
+ /* adds a node at i within the range of 0 > i > n */
65
+ add ( i , x ) {
66
+ if ( 0 > i || i > this . numberOfValues ) return ;
67
+ this . addBefore ( this . getNode ( i ) , x ) ;
48
68
}
49
69
50
- insertAfter ( data , toNodeData ) {
51
- let current = this . head ;
52
- while ( current ) {
53
- if ( current . data === toNodeData ) {
54
- const node = new Node ( data ) ;
55
- if ( current === this . tail ) {
56
- this . add ( data ) ;
57
- } else {
58
- current . next . previous = node ;
59
- node . previous = current ;
60
- node . next = current . next ;
61
- current . next = node ;
62
- this . numberOfValues ++ ;
63
- }
64
- }
65
- current = current . next ;
66
- }
70
+ /* this should be a private method */
71
+ removeNode ( x ) {
72
+ x . previous . next = x . next ;
73
+ x . next . previous = x . previous ;
74
+ this . numberOfValues -- ;
67
75
}
68
76
69
- traverse ( fn ) {
70
- let current = this . head ;
71
- while ( current ) {
72
- if ( fn ) {
73
- fn ( current ) ;
74
- }
75
- current = current . next ;
76
- }
77
+ /* remeoves a node within the range of 0 > i >= n */
78
+ remove ( i ) {
79
+ if ( 0 > i || i >= this . numberOfValues ) return undefined ;
80
+ let node = this . getNode ( i ) ;
81
+ this . removeNode ( node ) ;
82
+ return node . data ;
77
83
}
78
84
79
- traverseReverse ( fn ) {
80
- let current = this . tail ;
81
- while ( current ) {
85
+ /* apply a function to the entire data set */
86
+ map ( fn ) {
87
+ let current = this . dummy . next ;
88
+ for ( let i = 0 ; i < this . numberOfValues ; i ++ ) {
82
89
if ( fn ) {
83
- fn ( current ) ;
90
+ current . data = fn ( current . data ) ;
84
91
}
85
- current = current . previous ;
92
+ current = current . next ;
86
93
}
87
94
}
88
95
96
+ /* returns the size of the list */
89
97
length ( ) {
90
98
return this . numberOfValues ;
91
99
}
92
100
101
+ /* prints out the list */
93
102
print ( ) {
94
103
let string = '' ;
95
- let current = this . head ;
96
- while ( current ) {
104
+ let current = this . dummy . next ;
105
+ for ( let i = 0 ; i < this . numberOfValues ; i ++ ) {
97
106
string += `${ current . data } ` ;
98
107
current = current . next ;
99
108
}
100
109
console . log ( string . trim ( ) ) ;
101
110
}
102
111
}
103
112
113
+ /* tester data */
114
+
104
115
const doublyLinkedList = new DoublyLinkedList ( ) ;
116
+
117
+ doublyLinkedList . print ( ) ; // => ''
118
+ for ( let i = 0 ; i < 10 ; i ++ ) {
119
+ doublyLinkedList . add ( doublyLinkedList . length ( ) , i ) ;
120
+ }
121
+ doublyLinkedList . map ( function ( val ) { return 10 * val ; } ) ;
122
+ doublyLinkedList . print ( ) ; //=> 0 10 20 30 40 50 60 70 80 90
123
+
124
+ for ( let i = 0 ; i < doublyLinkedList . length ( ) ; i ++ ) {
125
+ console . log ( doublyLinkedList . get ( i ) ) ; //=> 0\n10\n20\n30\n40\n50\n60\n70\n80\n90\n
126
+ }
127
+ doublyLinkedList . remove ( 0 ) ;
128
+ doublyLinkedList . remove ( doublyLinkedList . length ( ) - 1 ) ;
129
+ doublyLinkedList . print ( ) ; // => 10 20 30 40 50 60 70 80
130
+ console . log ( doublyLinkedList . length ( ) ) ; // => 8
131
+
132
+ /* these line should all print out undefined */
133
+ console . log ( doublyLinkedList . get ( - 1 ) ) ;
134
+ console . log ( doublyLinkedList . get ( doublyLinkedList . length ( ) ) ) ;
135
+ console . log ( doublyLinkedList . set ( - 1 , 10 ) ) ;
136
+ console . log ( doublyLinkedList . set ( doublyLinkedList . length ( ) , 10 ) ) ;
137
+ console . log ( doublyLinkedList . add ( - 1 , 10 ) ) ;
138
+ console . log ( doublyLinkedList . add ( doublyLinkedList . length ( ) + 1 , 10 ) ) ;
139
+ doublyLinkedList . print ( ) ; // => 10 20 30 40 50 60 70 80
140
+ doublyLinkedList . add ( 0 , - 10 ) ;
141
+ doublyLinkedList . print ( ) ; // => -10 10 20 30 40 50 60 70 80
142
+
143
+ /*
105
144
doublyLinkedList.print(); // => ''
106
145
doublyLinkedList.add(1);
107
146
doublyLinkedList.add(2);
@@ -144,3 +183,4 @@ console.log('length is 7:', doublyLinkedList.length()); // => 7
144
183
doublyLinkedList.traverseReverse(node => { console.log(node.data); }); // => 18 17 16 15 14 13 12
145
184
doublyLinkedList.print(); // => 12 13 14 15 16 17 18
146
185
console.log('length is 7:', doublyLinkedList.length()); // => 7
186
+ */
0 commit comments