linted and prettiered

This commit is contained in:
ak 2023-06-27 23:06:21 -07:00
parent ffa48c45b0
commit 451e07a21b
5 changed files with 177 additions and 171 deletions

10
.eslintrc.cjs Normal file
View file

@ -0,0 +1,10 @@
module.exports = {
"extends": [
"eslint-config-prettier",
"prettier"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
}
}

View file

@ -1,5 +0,0 @@
module.exports = {
"extends": [
"eslint-config-prettier"
]
}

View file

@ -1,2 +1,3 @@
# js-linkedlist # js-linkedlist
Linked list implementation in ES6

View file

@ -5,8 +5,8 @@ export const LinkedList = () => {
const CLU = { const CLU = {
firstNode: null, firstNode: null,
lastNode: null, lastNode: null,
totalNodes: 0 totalNodes: 0,
} };
// adds a new node containing value to the end of the list // adds a new node containing value to the end of the list
const append = (value) => { const append = (value) => {
// make node // make node
@ -20,7 +20,7 @@ export const LinkedList = () => {
// else // else
CLU.lastNode.nextNode = CLU.lastNode = newNode; CLU.lastNode.nextNode = CLU.lastNode = newNode;
CLU.totalNodes++; CLU.totalNodes++;
} };
// adds a new node containing value to the start of the list // adds a new node containing value to the start of the list
const prepend = (value) => { const prepend = (value) => {
// set it to first node if we have no others // set it to first node if we have no others
@ -36,31 +36,31 @@ export const LinkedList = () => {
const newNode = Node(value, CLU.firstNode); const newNode = Node(value, CLU.firstNode);
CLU.firstNode = newNode; CLU.firstNode = newNode;
CLU.totalNodes++; CLU.totalNodes++;
} };
// returns the total number of nodes in the list // returns the total number of nodes in the list
const size = () => { const size = () => {
return CLU.totalNodes; return CLU.totalNodes;
} };
// returns the first node in the list // returns the first node in the list
const head = () => { const head = () => {
return CLU.firstNode; return CLU.firstNode;
} };
// returns the last node in the list // returns the last node in the list
const tail = () => { const tail = () => {
return CLU.lastNode; return CLU.lastNode;
} };
// returns the node at the given index // returns the node at the given index
const at = (index) => { const at = (index) => {
// set to first node // set to first node
const MCP = { const MCP = {
currentNode: CLU.firstNode currentNode: CLU.firstNode,
} };
// loop through LL to index // loop through LL to index
for (let i = 0; i < index; i++) { for (let i = 0; i < index; i++) {
MCP.currentNode = MCP.currentNode.nextNode; MCP.currentNode = MCP.currentNode.nextNode;
} }
return MCP.currentNode; return MCP.currentNode;
} };
// removes the last element from the list // removes the last element from the list
const pop = () => { const pop = () => {
// gets second to last node // gets second to last node
@ -73,17 +73,17 @@ export const LinkedList = () => {
if (CLU.totalNodes === 0) { if (CLU.totalNodes === 0) {
CLU.firstNode = CLU.lastNode = null; CLU.firstNode = CLU.lastNode = null;
} }
} };
// returns true if the passed in value is in the list and otherwise returns false. // returns true if the passed in value is in the list and otherwise returns false.
const contains = (value) => { const contains = (value) => {
find(value) ? true : false; find(value) ? true : false;
} };
// returns the index of the node containing value, or null if not found. !!!!!!!!!!! // returns the index of the node containing value, or null if not found. !!!!!!!!!!!
const find = (value) => { const find = (value) => {
// set to first node // set to first node
const MCP = { const MCP = {
currentNode: CLU.firstNode currentNode: CLU.firstNode,
} };
// for all nodes // for all nodes
for (let i = 0; i < CLU.totalNodes; i++) { for (let i = 0; i < CLU.totalNodes; i++) {
// if node value matches, set to true // if node value matches, set to true
@ -94,15 +94,15 @@ export const LinkedList = () => {
MCP.currentNode = MCP.currentNode.nextNode; MCP.currentNode = MCP.currentNode.nextNode;
} }
return null; return null;
} };
// print Nodes out and preview them in the console // print Nodes out and preview them in the console
const toString = () => { const toString = () => {
// final String value // final String value
let finalStr = ''; let finalStr = "";
// set to first node // set to first node
const MCP = { const MCP = {
currentNode: CLU.firstNode currentNode: CLU.firstNode,
} };
// loop through LL to index // loop through LL to index
for (let i = 0; i < CLU.totalNodes; i++) { for (let i = 0; i < CLU.totalNodes; i++) {
finalStr = finalStr + ` ( ${MCP.currentNode.value} ) ->`; finalStr = finalStr + ` ( ${MCP.currentNode.value} ) ->`;
@ -110,7 +110,7 @@ export const LinkedList = () => {
} }
finalStr = finalStr + " null"; finalStr = finalStr + " null";
return finalStr; return finalStr;
} };
// inserts a new node with the provided value at the given index // inserts a new node with the provided value at the given index
const insertAt = (value, index) => { const insertAt = (value, index) => {
if (index === 0) { if (index === 0) {
@ -135,7 +135,7 @@ export const LinkedList = () => {
prevNode.nextNode = newNode; prevNode.nextNode = newNode;
// updates counter // updates counter
CLU.totalNodes = CLU.totalNodes + 1; CLU.totalNodes = CLU.totalNodes + 1;
} };
// removes the node at the given index // removes the node at the given index
const removeAt = (index) => { const removeAt = (index) => {
// pops if final value // pops if final value
@ -158,7 +158,7 @@ export const LinkedList = () => {
if (CLU.totalNodes === 0) { if (CLU.totalNodes === 0) {
CLU.firstNode = CLU.lastNode = null; CLU.firstNode = CLU.lastNode = null;
} }
} };
return { return {
append, append,
prepend, prepend,
@ -171,6 +171,6 @@ export const LinkedList = () => {
find, find,
toString, toString,
insertAt, insertAt,
removeAt removeAt,
} };
} };

View file

@ -2,6 +2,6 @@
export const Node = (value = null, nextNode = null) => { export const Node = (value = null, nextNode = null) => {
return { return {
value, value,
nextNode nextNode,
} };
} };