README.md (10645B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Doubly Linked List 22 23 > Doubly linked list constructor. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var doublyLinkedList = require( '@stdlib/utils/doubly-linked-list' ); 41 ``` 42 43 #### doublyLinkedList() 44 45 Returns a new doubly linked list instance. 46 47 ```javascript 48 var list = doublyLinkedList(); 49 // returns <DoublyLinkedList> 50 ``` 51 52 ##### list.clear() 53 54 Clears a list. 55 56 ```javascript 57 var list = doublyLinkedList(); 58 // returns <DoublyLinkedList> 59 60 // Add values to the list: 61 list.push( 'foo' ).push( 'bar' ); 62 63 // Peek at the first value: 64 var v = list.first().value; 65 // returns 'foo' 66 67 // Examine the list length: 68 var len = list.length; 69 // returns 2 70 71 // Clear all list items: 72 list.clear(); 73 74 // Peek at the first value: 75 v = list.first(); 76 // returns undefined 77 78 // Examine the list length: 79 len = list.length; 80 // returns 0 81 ``` 82 83 ##### list.first() 84 85 Returns the first `node`. If the list is currently empty, the returned value is `undefined`. 86 87 ```javascript 88 var list = doublyLinkedList(); 89 // returns <DoublyLinkedList> 90 91 // Add values to the list: 92 list.push( 'foo' ).push( 'bar' ); 93 94 // Peek at the first value: 95 var v = list.first().value; 96 // returns 'foo' 97 ``` 98 99 ##### list.insert( node, value\[, location] ) 100 101 Inserts a `value` into the list either before or after a provided list `node`. 102 103 ```javascript 104 var list = doublyLinkedList(); 105 // returns <DoublyLinkedList> 106 107 // Add values to the list: 108 list.push( 'foo' ).push( 'bar' ).push( 'beep' ); 109 110 // Determine the list length: 111 var len = list.length; 112 // returns 3 113 114 // Get the second node: 115 var node = list.first().next; 116 117 // Insert a value after the second node: 118 list.insert( node, 'boop' ); 119 120 // Determine the list length: 121 len = list.length; 122 // returns 4 123 124 // Return a list of values: 125 var values = list.toArray(); 126 // returns [ 'foo', 'bar', 'boop', 'beep' ] 127 ``` 128 129 The method supports the following insertion locations: 130 131 - `'before'`: insert a `value` into the list **before** a provided list `node`. 132 - `'after'`: insert a `value` into the list **after** a provided list `node`. 133 134 By default, the method inserts a `value` into the list **after** a provided list `node`. To insert a value **before** a provided list `node`, invoke the method with the `location` argument equal to `'before'`. 135 136 ```javascript 137 var list = doublyLinkedList(); 138 // returns <DoublyLinkedList> 139 140 // Add values to the list: 141 list.push( 'foo' ).push( 'bar' ).push( 'beep' ); 142 143 // Determine the list length: 144 var len = list.length; 145 // returns 3 146 147 // Get the second node: 148 var node = list.first().next; 149 150 // Insert a value before the second node: 151 list.insert( node, 'boop', 'before' ); 152 153 // Determine the list length: 154 len = list.length; 155 // returns 4 156 157 // Return a list of values: 158 var values = list.toArray(); 159 // returns [ 'foo', 'boop', 'bar', 'beep' ] 160 ``` 161 162 ##### list.iterator( \[direction] ) 163 164 Returns an iterator for iterating over a list. If an environment supports `Symbol.iterator`, the returned iterator is iterable. 165 166 ```javascript 167 var list = doublyLinkedList(); 168 169 // Add values to the list: 170 list.push( 'foo' ).push( 'bar' ); 171 172 // Create an iterator: 173 var it = list.iterator(); 174 175 // Iterate over the list... 176 var v = it.next().value; 177 // returns 'foo' 178 179 v = it.next().value; 180 // returns 'bar' 181 182 var bool = it.next().done; 183 // returns true 184 ``` 185 186 The method supports the following iteration directions: 187 188 - `'forward'`: iterate over a list from the first value until the last value. 189 - `'reverse'`: iterate over a list from the last value until the first value. 190 191 By default, the method returns an iterator which iterates over a list from the first value until the last value. To return an iterator which iterates in reverse order, invoke the method with the `direction` argument equal to `'reverse'`. 192 193 ```javascript 194 var list = doublyLinkedList(); 195 196 // Add values to the list: 197 list.push( 'foo' ).push( 'bar' ); 198 199 // Create an iterator: 200 var it = list.iterator( 'reverse' ); 201 202 // Iterate over the list... 203 var v = it.next().value; 204 // returns 'bar' 205 206 v = it.next().value; 207 // returns 'foo' 208 209 var bool = it.next().done; 210 // returns true 211 ``` 212 213 **Note**: in order to prevent confusion arising from list mutation during iteration, a returned iterator **always** iterates over a list "snapshot", which is defined as the list of list elements at the time of `list.iterator()` invocation. 214 215 ##### list.last() 216 217 Returns the last `node`. If the list is currently empty, the returned value is `undefined`. 218 219 ```javascript 220 var list = doublyLinkedList(); 221 // returns <DoublyLinkedList> 222 223 // Add values to the list: 224 list.push( 'foo' ).push( 'bar' ); 225 226 // Peek at the last value: 227 var v = list.last().value; 228 // returns 'bar' 229 ``` 230 231 ##### list.length 232 233 List length. 234 235 ```javascript 236 var list = doublyLinkedList(); 237 238 // Examine the initial list length: 239 var len = list.length; 240 // returns 0 241 242 // Add values to the list: 243 list.push( 'foo' ).push( 'bar' ); 244 245 // Retrieve the current list length: 246 len = list.length; 247 // returns 2 248 ``` 249 250 ##### list.pop() 251 252 Removes a value from the end of the list. If the list is currently empty, the returned value is `undefined`. 253 254 ```javascript 255 var list = doublyLinkedList(); 256 257 // Add values to the list: 258 list.push( 'foo' ).push( 'bar' ); 259 260 // Remove the last value: 261 var v = list.pop(); 262 // returns 'bar' 263 264 // Add a new value to the list: 265 list.push( 'beep' ); 266 267 // Remove the last value: 268 v = list.pop(); 269 // returns 'beep' 270 ``` 271 272 ##### list.push( value ) 273 274 Adds a value to the end of the list. 275 276 ```javascript 277 var list = doublyLinkedList(); 278 279 // Add values to the list: 280 list.push( 'foo' ).push( 'bar' ); 281 282 // Remove the last value: 283 var v = list.pop(); 284 // returns 'bar' 285 286 // Add a new value to the list: 287 list.push( 'beep' ); 288 289 // Remove the last value: 290 v = list.pop(); 291 // returns 'beep' 292 ``` 293 294 ##### list.remove( node ) 295 296 Removes a `node` from the list. 297 298 ```javascript 299 var list = doublyLinkedList(); 300 301 // Add values to the list: 302 list.push( 'foo' ).push( 'bar' ).push( 'beep' ); 303 304 // Determine the list length: 305 var len = list.length; 306 // returns 3 307 308 // Get the second node: 309 var node = list.first().next; 310 311 // Remove the second node: 312 var v = list.remove( node ); 313 // returns 'bar' 314 315 // Determine the list length: 316 len = list.length; 317 // returns 2 318 ``` 319 320 ##### list.shift() 321 322 Removes a value from the beginning of the list. If the list is currently empty, the returned value is `undefined`. 323 324 ```javascript 325 var list = doublyLinkedList(); 326 327 // Add values to the list: 328 list.push( 'foo' ).push( 'bar' ); 329 330 // Remove the first value: 331 var v = list.shift(); 332 // returns 'foo' 333 334 // Add a new value to the list: 335 list.push( 'beep' ); 336 337 // Remove the first value: 338 v = list.shift(); 339 // returns 'bar' 340 ``` 341 342 ##### list.toArray() 343 344 Returns an array of list values. 345 346 ```javascript 347 var list = doublyLinkedList(); 348 349 // Add values to the list: 350 list.push( 'foo' ).push( 'bar' ); 351 352 // Get an array of list values: 353 var vals = list.toArray(); 354 // returns [ 'foo', 'bar' ] 355 ``` 356 357 ##### list.toJSON() 358 359 Serializes a list as JSON. 360 361 ```javascript 362 var list = doublyLinkedList(); 363 364 // Add values to the list: 365 list.push( 'foo' ).push( 'bar' ); 366 367 // Serialize to JSON: 368 var o = list.toJSON(); 369 // returns { 'type': 'doubly-linked-list', 'data': [ 'foo', 'bar' ] } 370 ``` 371 372 **Note**: `JSON.stringify()` implicitly calls this method when stringifying a list instance. 373 374 ##### list.unshift( value ) 375 376 Adds a value to the beginning of the list. 377 378 ```javascript 379 var list = doublyLinkedList(); 380 381 // Add values to the list: 382 list.unshift( 'foo' ).unshift( 'bar' ); 383 384 // Remove the last value: 385 var v = list.pop(); 386 // returns 'foo' 387 388 // Add a new value to the list: 389 list.unshift( 'beep' ); 390 391 // Remove the last value: 392 v = list.pop(); 393 // returns 'bar' 394 ``` 395 396 </section> 397 398 <!-- /.usage --> 399 400 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 401 402 <section class="notes"> 403 404 ## Notes 405 406 - To manually traverse a list, use list node `next` and `prev` properties. 407 408 ```javascript 409 var list = doublyLinkedList(); 410 411 // Add values to the list: 412 list.push( 'foo' ).push( 'bar' ).push( 'beep' ).push( 'boop' ); 413 414 // Get the first list node: 415 var n = list.first(); 416 417 // Walk the list forward... 418 while ( n ) { 419 console.log( n.value ); 420 n = n.next; 421 } 422 423 // Get the last list node: 424 n = list.last(); 425 426 // Walk the list backward... 427 while ( n ) { 428 console.log( n.value ); 429 n = n.prev; 430 } 431 ``` 432 433 </section> 434 435 <!-- /.notes --> 436 437 <!-- Package usage examples. --> 438 439 <section class="examples"> 440 441 ## Examples 442 443 <!-- eslint no-undef: "error" --> 444 445 ```javascript 446 var doublyLinkedList = require( '@stdlib/utils/doubly-linked-list' ); 447 448 var list; 449 var iter; 450 var len; 451 var v; 452 var i; 453 454 // Create a new doubly linked list: 455 list = doublyLinkedList(); 456 457 // Add some values to the list: 458 list.push( 'foo' ); 459 list.push( 'bar' ); 460 list.push( 'beep' ); 461 list.push( 'boop' ); 462 463 // Peek at the first and last list values: 464 v = list.first().value; 465 // returns 'foo' 466 467 v = list.last().value; 468 // returns 'boop' 469 470 // Inspect the list length: 471 len = list.length; 472 // returns 4 473 474 // Remove the last list value: 475 v = list.pop(); 476 // returns 'boop' 477 478 // Inspect the list length: 479 len = list.length; 480 // returns 3 481 482 // Iterate over the list: 483 iter = list.iterator(); 484 for ( i = 0; i < len; i++ ) { 485 console.log( 'List value #%d: %s', i+1, iter.next().value ); 486 } 487 488 // Clear the list: 489 list.clear(); 490 491 // Inspect the list length: 492 len = list.length; 493 // returns 0 494 ``` 495 496 </section> 497 498 <!-- /.examples --> 499 500 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 501 502 <section class="references"> 503 504 </section> 505 506 <!-- /.references --> 507 508 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 509 510 <section class="links"> 511 512 </section> 513 514 <!-- /.links -->