Skip to content

Commit

Permalink
document: LinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 26, 2023
1 parent a881a19 commit 1ac34b9
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 15 deletions.
221 changes: 209 additions & 12 deletions src/DataStructures/LinkedList.js
@@ -1,14 +1,32 @@
export class Node {
/**
* Represents a node in a linked list.
*
* @memberof DataStructures
*/
class LLNode {
/**
* Creates a new Node instance.
* @param {*} value - The value to be stored in the node.
*/
constructor(value) {
this.value = value;
this.next = null;
}
}

export class LinkedList {
/**
* Represents a linked list data structure.
*
* @memberof DataStructures
*/
class LinkedList {
/**
* Creates a new LinkedList instance.
* @param {*} value - The value to initialize the list with (optional).
*/
constructor(value) {
if (value !== undefined) {
const newNode = new Node(value);
const newNode = new LLNode(value);
this.head = newNode;
this.tail = newNode;
this.size = 1;
Expand All @@ -19,6 +37,21 @@ export class LinkedList {
}
}

/**
* Prints the values of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push("Apple");
* ll.push("Banana");
* ll.push("Cherry");
*
* ll.print(); // Apple, Banana, Cherry
*
* @returns {LinkedList} The current LinkedList instance.
*
* @memberof LinkedList
*/
print() {
let temp = this.head;
while (temp !== null) {
Expand All @@ -28,13 +61,37 @@ export class LinkedList {
return this;
}

/**
* Clears the linked list, removing all elements.
*
* @returns {LinkedList} The current LinkedList instance.
*
* @memberof LinkedList
*/
clear() {
this.head = null;
this.tail = null;
this.size = 0;
return this;
}

/**
* Retrieves the value at the specified index in the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.get(1); // 10
*
* @param {number} index - The index of the value to retrieve.
* @param {boolean} [returnNode=false] - Specifies whether to return the Node instead of the value.
* @returns {*} The value at the specified index, or undefined if index is out of bounds.
*
* @memberof LinkedList
*/
get(index, returnNode = false) {
if (index < 0 || index >= this.size) return undefined;

Expand All @@ -49,6 +106,23 @@ export class LinkedList {
return temp; // Should never reach here
}

/**
* Sets the value at the specified index in the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.set(1, 0); // 0
*
* @param {number} index - The index of the value to set.
* @param {*} value - The new value to set.
* @returns {boolean} True if the value was set successfully, false otherwise.
*
* @memberof LinkedList
*/
set(index, value) {
const node = this.get(index, true);
if (!node) return false;
Expand All @@ -57,9 +131,25 @@ export class LinkedList {
return true;
}

// ADD a new value to the START of the list
/**
* Adds a new value at the start of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.unshift(0); // 0,10,20,30
*
*
* @param {*} value - The value to add.
* @returns {LinkedList} The current LinkedList instance.
*
* @memberof LinkedList
*/
unshift(value) {
const newNode = new Node(value);
const newNode = new LLNode(value);
newNode.next = this.head;
this.head = newNode;

Expand All @@ -70,9 +160,22 @@ export class LinkedList {
return this;
}

// ADD a new value to the END of the list
/**
* Adds a new value at the end of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10); // 10
* ll.push(20); // 10,20
* ll.push(30); // 10,20,30
*
* @param {*} value - The value to add.
* @returns {LinkedList} The current LinkedList instance.
*
* @memberof LinkedList
*/
push(value) {
const newNode = new Node(value);
const newNode = new LLNode(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
Expand All @@ -85,13 +188,29 @@ export class LinkedList {
return this;
}

// ADD a new value to the given index of the list
/**
* Adds a new value at the given index of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.insert(1,55); // 10,55,20,30
*
* @param {number} index - The index at which to add the value.
* @param {*} value - The value to add.
* @returns {boolean} True if the value was added successfully, false otherwise.
*
* @memberof LinkedList
*/
insert(index, value) {
if (index === 0) return this.unshift(value);
else if (index === this.size) return this.push(value);
else if (index < 0 || index > this.size) return false;

const newNode = new Node(value);
const newNode = new LLNode(value);
const temp = this.get(index - 1, true);

newNode.next = temp.next;
Expand All @@ -101,7 +220,21 @@ export class LinkedList {
return this;
}

// REMOVE a value to the START of the list
/**
* Removes the value at the start of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.shift(); // 20,30
*
* @returns {*} The removed value, or undefined if the list is empty.
*
* @memberof LinkedList
*/
shift() {
if (!this.size) return undefined;

Expand All @@ -115,7 +248,21 @@ export class LinkedList {
return removedValue;
}

// REMOVE a value from the END of the list
/**
* Removes the value from the end of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.pop(); // 10,20
*
* @returns {*} The removed value, or undefined if the list is empty.
*
* @memberof LinkedList
*/
pop() {
if (!this.size) return undefined;

Expand All @@ -135,7 +282,22 @@ export class LinkedList {
return temp.value;
}

// REMOVE a value in the given index of the list
/**
* Removes the value at the given index of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.shift(1); // 10,30
*
* @param {number} index - The index at which to remove the value.
* @returns {*} The removed value, or undefined if the index is out of bounds.
*
* @memberof LinkedList
*/
remove(index) {
if (index === 0) return this.shift();
else if (index === this.size) return this.pop();
Expand All @@ -150,6 +312,21 @@ export class LinkedList {
return temp.value;
}

/**
* Reverses the order of the linked list.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.reverse(); // 30,20,10
*
* @returns {LinkedList} The current LinkedList instance.
*
* @memberof LinkedList
*/
reverse() {
let temp = this.head;
this.head = this.tail;
Expand All @@ -168,6 +345,21 @@ export class LinkedList {
return this;
}

/**
* Converts the linked list to an array.
*
* @example
* const ll = new LinkedList();
* ll.push(10);
* ll.push(20);
* ll.push(30);
*
* ll.toArray(); // [10,20,30]
*
* @returns {Array} An array containing the values of the linked list.
*
* @memberof LinkedList
*/
toArray() {
const result = [];

Expand All @@ -180,3 +372,8 @@ export class LinkedList {
return result;
}
}

export {
LLNode,
LinkedList,
};
6 changes: 3 additions & 3 deletions tests/DataStructures/LinkedList.test.js
@@ -1,4 +1,4 @@
import { Node } from "../../src/DataStructures/LinkedList.js";
import { LLNode } from "../../src/DataStructures/LinkedList.js";
import { DataStructures } from "../../src/index.js";
const { LinkedList } = DataStructures;

Expand All @@ -8,11 +8,11 @@ describe("LinkedList.js", () => {
it("creates a new LinkedList", () => {
const ll = new LinkedList(100);

expect(ll.head).toBeInstanceOf(Node);
expect(ll.head).toBeInstanceOf(LLNode);
expect(ll.head.value).toBe(100);
expect(ll.head.next).toBe(null);

expect(ll.tail).toBeInstanceOf(Node);
expect(ll.tail).toBeInstanceOf(LLNode);
expect(ll.tail.value).toBe(100);
expect(ll.tail.next).toBe(null);

Expand Down

0 comments on commit 1ac34b9

Please sign in to comment.