The fhf-linkedlist
library provides a powerful and efficient implementation of a linked list using WebAssembly for performance-critical operations.
To install the fhf-linkedlist
library, use npm:
npm install fhf-linkedlist
Here's a quick example to get you started with fhf-linkedlist
:
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([1, 2, 3]);
await list.append(4);
await list.display(); // Outputs: 1 -> 2 -> 3 -> 4
})();
Adds a new element to the end of the list.
-
Parameters:
-
data
: The data to append.
-
-
Usage:
await list.append(5);
Adds a new element to the beginning of the list.
-
Parameters:
-
data
: The data to push.
-
-
Usage:
await list.push(0);
Displays the elements of the list.
-
Usage:
await list.display();
Returns the number of elements in the list.
-
Returns:
-
Number
: The length of the list.
-
-
Usage:
const len = await list.length(); console.log(len); // Outputs: 4
Reverses the list.
-
Usage:
await list.reverse();
Frees the memory allocated for the list.
-
Usage:
await list.freeList();
Gets the value at the specified index.
-
Parameters:
-
index
: The index of the element to retrieve.
-
-
Returns:
-
Any
: The value at the specified index.
-
-
Usage:
const value = await list.get(2); console.log(value); // Outputs: 3
Deletes the node at the specified index.
-
Parameters:
-
index
: The index of the node to delete.
-
-
Usage:
await list.deleteNode(1);
Inserts a new element at the specified index.
-
Parameters:
-
index
: The index at which to insert the new element. -
data
: The data to insert.
-
-
Usage:
await list.insert(1, 10);
Sorts the elements of the list.
-
Usage:
await list.sort();
Converts the linked list to an array.
-
Returns:
-
Array
: The array representation of the linked list.
-
-
Usage:
const array = await list.toArray(); console.log(array); // Outputs: [6, 5]
The linked list utilizes WebAssembly memory for efficient allocation and manipulation. The memory is configured with an initial size and a maximum size to handle the linked list operations.
The library uses WebAssembly to perform core linked list operations such as appending, pushing, deleting, and reversing nodes. The main.wasm
file is loaded and instantiated, providing access to the exported functions used within the LinkedList
class.
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([1, 2, 3]);
await list.display(); // Outputs: 1 -> 2 -> 3
await list.freeList();
})();
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([1, 2, 3]);
await list.append(4);
await list.push(0);
await list.display(); // Outputs: 0 -> 1 -> 2 -> 3 -> 4
await list.freeList();
})();
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([1, 2, 3]);
await list.reverse();
await list.display(); // Outputs: 3 -> 2 -> 1
await list.freeList();
})();
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([9, 2, 10, 4, 5]);
const length = await list.length();
console.log(length);
await list.freeList();
})();
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([3, 1, 4, 1, 5]);
await list.sort();
await list.display(); // Outputs: 1 -> 1 -> 3 -> 4 -> 5
await list.freeList();
})();
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList();
await list.push(5);
await list.push(6);
console.log(await list.toArray()); // Outputs: [6, 5]
await list.freeList();
})();
import LinkedList from "fhf-linkedlist";
(async () => {
const list = new LinkedList([1, 2, 3, 4, 5]);
await list.freeList();
await list.display(); // Outputs nothing, as the list is now empty
})();
The fhf-linkedlist
library offers a high-performance linked list implementation with the power of WebAssembly. This documentation provides a comprehensive guide to using the library, focusing on the index.js
file. For further assistance, refer to the additional examples and method descriptions provided.