singly-linked-list-utils
v1.0.0
Published
A lightweight JavaScript library for Singly Linked Lists
Maintainers
Readme
SSL Utils
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.
✨ Features
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
Installation
npm install ssl-utilsUsage
const { SinglyLinkedList } = require("ssl-utils");Create a list from an array.
const list = SinglyLinkedList.from([10, 20, 30]);
console.log(list.toArray());
// [10, 20, 30]API
Create
SinglyLinkedList.from(array)
Creates a new linked list from an array.
const list = SinglyLinkedList.from([1, 2, 3]);Insert
insertFront(value)
Insert a node at the beginning.
list.insertFront(5);Before
10 -> 20 -> 30After
5 -> 10 -> 20 -> 30insertBack(value)
Insert at the end.
list.insertBack(40);Before
10 -> 20 -> 30After
10 -> 20 -> 30 -> 40insertAt(value, index)
Insert at any valid index.
list.insertAt(25, 2);Before
10 -> 20 -> 30After
10 -> 20 -> 25 -> 30Delete
deleteFront()
Deletes the first node.
list.deleteFront();deleteBack()
Deletes the last node.
list.deleteBack();deleteAt(index)
Deletes node at the given index.
list.deleteAt(2);deleteValue(value)
Deletes the first occurrence of the given value.
list.deleteValue(20);Example
Before
10 -> 20 -> 30 -> 20After
10 -> 30 -> 20deleteAllValues(value)
Deletes all occurrences of a value.
list.deleteAllValues(20);Before
10 -> 20 -> 30 -> 20After
10 -> 30deleteList()
Deletes the entire linked list.
list.deleteList();Conversion
toArray()
Returns an array representation.
list.toArray();Output
[10, 20, 30]toString()
Returns a printable string.
list.toString();Output
10 -> 20 -> 30print()
Prints the linked list.
list.print();Output
10 -> 20 -> 30Length
length()
Returns the number of nodes.
console.log(list.length());Output
3Example
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]
3Error Handling
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
Supported Data Types
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 }
]);Testing
The project uses Jest for unit testing.
Run all tests:
npm testRun tests with verbose output:
npm test -- --verboseProject Structure
ssl-utils
│
├── src
│ ├── Node.js
│ ├── SinglyLinkedList.js
│ └── index.js
│
├── tests
│
├── examples
│
├── README.md
├── LICENSE
├── package.json
└── .gitignoreContributing
Contributions, feature requests, bug reports, and improvements are welcome.
Feel free to fork the repository and submit a pull request.
License
This project is licensed under the MIT License.
Roadmap
Version 1.0.0
- ✅ Create from array
- ✅ Insert operations
- ✅ Delete operations
- ✅ Conversion utilities
- ✅ Length
Planned
- ⬜ Reverse
- ⬜ Find
- ⬜ Middle
- ⬜ Sort
- ⬜ Doubly Linked List support
