Skip to content

Commit 183df0c

Browse files
authored
New: CircularLinkedList implementation
* New: CircularLinkedList implementation * Fix: Typo in README
1 parent 3dd450c commit 183df0c

File tree

5 files changed

+1107
-1
lines changed

5 files changed

+1107
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Collection of classic computer science paradigms, algorithms, and approaches wri
1313
The most recent packages are found in these directories:
1414

1515
* `src` - the implementation source code
16-
* `tests`` - tests for the implementation source code
16+
* `tests` - tests for the implementation source code
1717

1818
These directories contain **old** implementations that will be replaced eventually, they are just here to avoid confusing people who find this repo through the old blog posts:
1919

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# JavaScript Circular 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 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 `CircularLinkedList` constructor:
19+
20+
```js
21+
const { CircularLinkedList } = require("@humanwhocodes/circular-linked-list");
22+
```
23+
24+
Each instance of `CircularLinkedList` has the following properties and methods:
25+
26+
```js
27+
const list = new CircularLinkedList();
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+
// remove an item at the given index and return the data that was removed
53+
let data = list.remove(0);
54+
55+
// remove all items
56+
list.clear();
57+
```
58+
59+
## Note on Code Style
60+
61+
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.
62+
63+
## Issues and Pull Requests
64+
65+
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.
66+
67+
## License
68+
69+
MIT

0 commit comments

Comments
 (0)