From 451e07a21bd24c5b4973bb3dee558424ca67838e Mon Sep 17 00:00:00 2001 From: ak Date: Tue, 27 Jun 2023 23:06:21 -0700 Subject: [PATCH] linted and prettiered --- .eslintrc.cjs | 10 ++ .eslintrc.js | 5 - README.md | 1 + src/LinkedList.js | 320 +++++++++++++++++++++++----------------------- src/Node.js | 12 +- 5 files changed, 177 insertions(+), 171 deletions(-) create mode 100644 .eslintrc.cjs delete mode 100644 .eslintrc.js diff --git a/.eslintrc.cjs b/.eslintrc.cjs new file mode 100644 index 0000000..cb55b61 --- /dev/null +++ b/.eslintrc.cjs @@ -0,0 +1,10 @@ +module.exports = { + "extends": [ + "eslint-config-prettier", + "prettier" + ], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module", + } +} diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 051f118..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - "extends": [ - "eslint-config-prettier" - ] -} diff --git a/README.md b/README.md index 1693aae..0b2e688 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # js-linkedlist +Linked list implementation in ES6 \ No newline at end of file diff --git a/src/LinkedList.js b/src/LinkedList.js index 43a896e..8106449 100644 --- a/src/LinkedList.js +++ b/src/LinkedList.js @@ -2,175 +2,175 @@ import { Node } from "./Node.js"; // linked list factory export const LinkedList = () => { - const CLU = { - firstNode: null, - lastNode: null, - totalNodes: 0 + const CLU = { + firstNode: null, + lastNode: null, + totalNodes: 0, + }; + // adds a new node containing value to the end of the list + const append = (value) => { + // make node + const newNode = Node(value); + // set it to first node if we have no others + if (CLU.totalNodes === 0) { + CLU.firstNode = CLU.lastNode = newNode; + CLU.totalNodes++; + return; } - // adds a new node containing value to the end of the list - const append = (value) => { - // make node - const newNode = Node(value); - // set it to first node if we have no others - if (CLU.totalNodes === 0) { - CLU.firstNode = CLU.lastNode = newNode; - CLU.totalNodes++; - return; - } - // else - CLU.lastNode.nextNode = CLU.lastNode = newNode; - CLU.totalNodes++; + // else + CLU.lastNode.nextNode = CLU.lastNode = newNode; + CLU.totalNodes++; + }; + // adds a new node containing value to the start of the list + const prepend = (value) => { + // set it to first node if we have no others + if (CLU.totalNodes === 0) { + // make node + const newNode = Node(value); + CLU.firstNode = CLU.lastNode = newNode; + CLU.totalNodes++; + return; } - // adds a new node containing value to the start of the list - const prepend = (value) => { - // set it to first node if we have no others - if (CLU.totalNodes === 0) { - // make node - const newNode = Node(value); - CLU.firstNode = CLU.lastNode = newNode; - CLU.totalNodes++; - return; - } - // else - // make node - const newNode = Node(value, CLU.firstNode); - CLU.firstNode = newNode; - CLU.totalNodes++; + // else + // make node + const newNode = Node(value, CLU.firstNode); + CLU.firstNode = newNode; + CLU.totalNodes++; + }; + // returns the total number of nodes in the list + const size = () => { + return CLU.totalNodes; + }; + // returns the first node in the list + const head = () => { + return CLU.firstNode; + }; + // returns the last node in the list + const tail = () => { + return CLU.lastNode; + }; + // returns the node at the given index + const at = (index) => { + // set to first node + const MCP = { + currentNode: CLU.firstNode, + }; + // loop through LL to index + for (let i = 0; i < index; i++) { + MCP.currentNode = MCP.currentNode.nextNode; } - // returns the total number of nodes in the list - const size = () => { - return CLU.totalNodes; + return MCP.currentNode; + }; + // removes the last element from the list + const pop = () => { + // gets second to last node + const penultimateNode = at(CLU.totalNodes - 2); + // erases from chain + penultimateNode.nextNode = null; + // updates node count + CLU.totalNodes = CLU.totalNodes - 1; + // catches the offchance + if (CLU.totalNodes === 0) { + CLU.firstNode = CLU.lastNode = null; } - // returns the first node in the list - const head = () => { - return CLU.firstNode; + }; + // returns true if the passed in value is in the list and otherwise returns false. + const contains = (value) => { + find(value) ? true : false; + }; + // returns the index of the node containing value, or null if not found. !!!!!!!!!!! + const find = (value) => { + // set to first node + const MCP = { + currentNode: CLU.firstNode, + }; + // for all nodes + for (let i = 0; i < CLU.totalNodes; i++) { + // if node value matches, set to true + if (MCP.currentNode.value === value) { + return i; + } + // otherwise set to next node + MCP.currentNode = MCP.currentNode.nextNode; } - // returns the last node in the list - const tail = () => { - return CLU.lastNode; + return null; + }; + // print Nodes out and preview them in the console + const toString = () => { + // final String value + let finalStr = ""; + // set to first node + const MCP = { + currentNode: CLU.firstNode, + }; + // loop through LL to index + for (let i = 0; i < CLU.totalNodes; i++) { + finalStr = finalStr + ` ( ${MCP.currentNode.value} ) ->`; + MCP.currentNode = MCP.currentNode.nextNode; } - // returns the node at the given index - const at = (index) => { - // set to first node - const MCP = { - currentNode: CLU.firstNode - } - // loop through LL to index - for (let i = 0; i < index; i++) { - MCP.currentNode = MCP.currentNode.nextNode; - } - return MCP.currentNode; + finalStr = finalStr + " null"; + return finalStr; + }; + // inserts a new node with the provided value at the given index + const insertAt = (value, index) => { + if (index === 0) { + prepend(value); + return; } - // removes the last element from the list - const pop = () => { - // gets second to last node - const penultimateNode = at(CLU.totalNodes - 2); - // erases from chain - penultimateNode.nextNode = null; - // updates node count - CLU.totalNodes = CLU.totalNodes - 1; - // catches the offchance - if (CLU.totalNodes === 0) { - CLU.firstNode = CLU.lastNode = null; - } + if (index === CLU.totalNodes) { + append(value); + return; } - // returns true if the passed in value is in the list and otherwise returns false. - const contains = (value) => { - find(value) ? true : false; + if (index === CLU.totalNodes + 1) { + console.log("Your index value is too high!"); + return; } - // returns the index of the node containing value, or null if not found. !!!!!!!!!!! - const find = (value) => { - // set to first node - const MCP = { - currentNode: CLU.firstNode - } - // for all nodes - for (let i = 0; i < CLU.totalNodes; i++) { - // if node value matches, set to true - if (MCP.currentNode.value === value) { - return i; - } - // otherwise set to next node - MCP.currentNode = MCP.currentNode.nextNode; - } - return null; + // gets node at previous index + const prevNode = at(index - 1); + // gets node at current index + const currNode = at(index); + // makes new node, sets current node at index as next + const newNode = Node(value, currNode); + // sets itself as previous index node's next node + prevNode.nextNode = newNode; + // updates counter + CLU.totalNodes = CLU.totalNodes + 1; + }; + // removes the node at the given index + const removeAt = (index) => { + // pops if final value + if (index === CLU.totalNodes - 1) { + pop(); + return; } - // print Nodes out and preview them in the console - const toString = () => { - // final String value - let finalStr = ''; - // set to first node - const MCP = { - currentNode: CLU.firstNode - } - // loop through LL to index - for (let i = 0; i < CLU.totalNodes; i++) { - finalStr = finalStr + ` ( ${MCP.currentNode.value} ) ->`; - MCP.currentNode = MCP.currentNode.nextNode; - } - finalStr = finalStr + " null"; - return finalStr; + if (index === 0) { + CLU.firstNode = CLU.firstNode.nextNode; + CLU.totalNodes = CLU.totalNodes - 1; + return; } - // inserts a new node with the provided value at the given index - const insertAt = (value, index) => { - if (index === 0) { - prepend(value); - return; - } - if (index === CLU.totalNodes) { - append(value); - return; - } - if (index === CLU.totalNodes + 1) { - console.log("Your index value is too high!"); - return; - } - // gets node at previous index - const prevNode = at(index - 1); - // gets node at current index - const currNode = at(index); - // makes new node, sets current node at index as next - const newNode = Node(value, currNode); - // sets itself as previous index node's next node - prevNode.nextNode = newNode; - // updates counter - CLU.totalNodes = CLU.totalNodes + 1; + // gets node at previous index + const prevNode = at(index - 1); + // sets its next node to the one ahead of the current one, effectively removing it from the chain + prevNode.nextNode = at(index + 1); + // removes a digit from the counter + CLU.totalNodes = CLU.totalNodes - 1; + // catches the offchance + if (CLU.totalNodes === 0) { + CLU.firstNode = CLU.lastNode = null; } - // removes the node at the given index - const removeAt = (index) => { - // pops if final value - if (index === CLU.totalNodes - 1) { - pop(); - return; - } - if (index === 0) { - CLU.firstNode = CLU.firstNode.nextNode; - CLU.totalNodes = CLU.totalNodes - 1; - return; - } - // gets node at previous index - const prevNode = at(index - 1); - // sets its next node to the one ahead of the current one, effectively removing it from the chain - prevNode.nextNode = at(index + 1); - // removes a digit from the counter - CLU.totalNodes = CLU.totalNodes - 1; - // catches the offchance - if (CLU.totalNodes === 0) { - CLU.firstNode = CLU.lastNode = null; - } - } - return { - append, - prepend, - size, - head, - tail, - at, - pop, - contains, - find, - toString, - insertAt, - removeAt - } -} \ No newline at end of file + }; + return { + append, + prepend, + size, + head, + tail, + at, + pop, + contains, + find, + toString, + insertAt, + removeAt, + }; +}; diff --git a/src/Node.js b/src/Node.js index 051f813..eb700fa 100644 --- a/src/Node.js +++ b/src/Node.js @@ -1,7 +1,7 @@ // node factory -export const Node = (value = null, nextNode = null) => { - return { - value, - nextNode - } -} \ No newline at end of file +export const Node = (value = null, nextNode = null) => { + return { + value, + nextNode, + }; +};