|
| 1 | +# JavaScript Circular Doubly Linked List Class |
| 2 | + |
| 3 | +by [Nicholas C. Zakas](https://humanwhocodes.com) |
| 4 | + |
| 5 | +If you find this useful, please consider supporting my work with a [donation](https://humanwhocodes.com/donate). |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +A JavaScript implementation of a circular doubly linked list. This class uses the conventions of built-in JavaScript collection objects, such as: |
| 10 | + |
| 11 | +1. There is a `[Symbol.iterator]` method so each instance is iterable. |
| 12 | +1. The `size` getter property instead of a `length` data property to indicate that the size of the list is dynamically counted rather than stored. |
| 13 | +1. Defining a `values()` generator method. |
| 14 | +1. Returning `undefined` from `get()` when no such index exists. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Use CommonJS to get access to the `CircularDoublyLinkedList` constructor: |
| 19 | + |
| 20 | +```js |
| 21 | +const { CircularDoublyLinkedList } = require("@humanwhocodes/circular-doubly-linked-list"); |
| 22 | +``` |
| 23 | + |
| 24 | +Each instance of `CircularDoublyLinkedList` has the following properties and methods: |
| 25 | + |
| 26 | +```js |
| 27 | +const list = new CircularDoublyLinkedList(); |
| 28 | + |
| 29 | +// add an item to the end |
| 30 | +list.add("foo"); |
| 31 | + |
| 32 | +// insert an item |
| 33 | +list.insertBefore("bar", 0); |
| 34 | +list.insertAfter("baz", 1); |
| 35 | + |
| 36 | +// get the value at an index |
| 37 | +let value = list.get(0); |
| 38 | + |
| 39 | +// get the number of items |
| 40 | +let count = list.size; |
| 41 | + |
| 42 | +// get the index of a value |
| 43 | +let index = list.indexOf("foo"); |
| 44 | + |
| 45 | +// convert to an array using iterators |
| 46 | +let array1 = [...list.values()]; |
| 47 | +let array2 = [...list]; |
| 48 | + |
| 49 | +// create a circular iterator to keep iterating over values |
| 50 | +const iterator = list.circularValues(); |
| 51 | + |
| 52 | +// convert to an array in reverse order using an iterator |
| 53 | +let array3 = [...list.reverse()]; |
| 54 | + |
| 55 | +// remove an item at the given index and return the data that was removed |
| 56 | +let data = list.remove(0); |
| 57 | + |
| 58 | +// remove all items |
| 59 | +list.clear(); |
| 60 | +``` |
| 61 | + |
| 62 | +## Note on Code Style |
| 63 | + |
| 64 | +You may find the code style of this module to be overly verbose with a lot of comments. That is intentional, as the primary use of this module is intended to be for educational purposes. There are frequently more concise ways of implementing the details of this class, but the more concise ways are difficult for newcomers who are unfamiliar with linked lists as a concept or JavaScript as a whole. |
| 65 | + |
| 66 | +## Issues and Pull Requests |
| 67 | + |
| 68 | +As this is part of series of tutorials I'm writing, only bug fixes will be accepted. No new functionality will be added to this module. |
| 69 | + |
| 70 | +## License |
| 71 | + |
| 72 | +MIT |
0 commit comments