A lightweight, beginner-friendly JavaScript library for working with Singly Linked Lists.
The goal of this project is to provide an easy-to-use implementation of a singly linked list with common operations while keeping the API simple and intuitive.
Current version supports:
- ✅ Create a linked list from an array
- ✅ Convert linked list to an array
- ✅ Insert at front
- ✅ Insert at back
- ✅ Insert at any index
- ✅ Delete front node
- ✅ Delete last node
- ✅ Delete node at an index
- ✅ Delete first occurrence of a value
- ✅ Delete all occurrences of a value
- ✅ Delete the entire linked list
- ✅ Print linked list
- ✅ Convert linked list to string
- ✅ Get length of linked list
Upcoming features:
- ⬜ Reverse
- ⬜ Find
- ⬜ Middle
- ⬜ Sort
npm install ssl-utilsconst { SinglyLinkedList } = require("ssl-utils");Create a list from an array.
const list = SinglyLinkedList.from([10, 20, 30]);
console.log(list.toArray());
// [10, 20, 30]Creates a new linked list from an array.
const list = SinglyLinkedList.from([1, 2, 3]);Insert a node at the beginning.
list.insertFront(5);Before
10 -> 20 -> 30
After
5 -> 10 -> 20 -> 30
Insert at the end.
list.insertBack(40);Before
10 -> 20 -> 30
After
10 -> 20 -> 30 -> 40
Insert at any valid index.
list.insertAt(25, 2);Before
10 -> 20 -> 30
After
10 -> 20 -> 25 -> 30
Deletes the first node.
list.deleteFront();Deletes the last node.
list.deleteBack();Deletes node at the given index.
list.deleteAt(2);Deletes the first occurrence of the given value.
list.deleteValue(20);Example
Before
10 -> 20 -> 30 -> 20
After
10 -> 30 -> 20
Deletes all occurrences of a value.
list.deleteAllValues(20);Before
10 -> 20 -> 30 -> 20
After
10 -> 30
Deletes the entire linked list.
list.deleteList();Returns an array representation.
list.toArray();Output
[10, 20, 30]Returns a printable string.
list.toString();Output
10 -> 20 -> 30
Prints the linked list.
list.print();Output
10 -> 20 -> 30
Returns the number of nodes.
console.log(list.length());Output
3
const { SinglyLinkedList } = require("ssl-utils");
const list = SinglyLinkedList.from([10, 20, 30]);
list
.insertFront(5)
.insertBack(40)
.insertAt(25, 3);
console.log(list.toString());
list.deleteFront();
list.deleteBack();
list.deleteValue(25);
console.log(list.toArray());
console.log(list.length());Output
5 -> 10 -> 20 -> 25 -> 30 -> 40
[10, 20, 30]
3
The library throws descriptive errors for invalid operations.
Examples:
- Input must be an array
- Value cannot be null or undefined
- Index must be a non-negative integer
- Index must be a non-negative integer and less than size
- List is empty
- Value not found in the list
The linked list can store any JavaScript value, including:
- Integers
- Floating-point numbers
- Strings
- Booleans
- Objects
- Arrays
- Functions
- Custom classes
Example
const list = SinglyLinkedList.from([
1,
3.14,
"SSL Utils",
true,
{ id: 1 }
]);The project uses Jest for unit testing.
Run all tests:
npm testRun tests with verbose output:
npm test -- --verbosessl-utils
│
├── src
│ ├── Node.js
│ ├── SinglyLinkedList.js
│ └── index.js
│
├── tests
│
├── examples
│
├── README.md
├── LICENSE
├── package.json
└── .gitignore
Contributions, feature requests, bug reports, and improvements are welcome.
Feel free to fork the repository and submit a pull request.
This project is licensed under the MIT License.
- ✅ Create from array
- ✅ Insert operations
- ✅ Delete operations
- ✅ Conversion utilities
- ✅ Length
- ⬜ Reverse
- ⬜ Find
- ⬜ Middle
- ⬜ Sort
- ⬜ Doubly Linked List support