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

@ -2,175 +2,175 @@ import { Node } from "./Node.js";
// linked list factory // linked list factory
export const LinkedList = () => { 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
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 // else
const append = (value) => { CLU.lastNode.nextNode = CLU.lastNode = newNode;
// make node CLU.totalNodes++;
const newNode = Node(value); };
// set it to first node if we have no others // adds a new node containing value to the start of the list
if (CLU.totalNodes === 0) { const prepend = (value) => {
CLU.firstNode = CLU.lastNode = newNode; // set it to first node if we have no others
CLU.totalNodes++; if (CLU.totalNodes === 0) {
return; // make node
} const newNode = Node(value);
// else CLU.firstNode = CLU.lastNode = newNode;
CLU.lastNode.nextNode = CLU.lastNode = newNode; CLU.totalNodes++;
CLU.totalNodes++; return;
} }
// adds a new node containing value to the start of the list // else
const prepend = (value) => { // make node
// set it to first node if we have no others const newNode = Node(value, CLU.firstNode);
if (CLU.totalNodes === 0) { CLU.firstNode = newNode;
// make node CLU.totalNodes++;
const newNode = Node(value); };
CLU.firstNode = CLU.lastNode = newNode; // returns the total number of nodes in the list
CLU.totalNodes++; const size = () => {
return; return CLU.totalNodes;
} };
// else // returns the first node in the list
// make node const head = () => {
const newNode = Node(value, CLU.firstNode); return CLU.firstNode;
CLU.firstNode = newNode; };
CLU.totalNodes++; // 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 return MCP.currentNode;
const size = () => { };
return CLU.totalNodes; // 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 = () => { // returns true if the passed in value is in the list and otherwise returns false.
return CLU.firstNode; 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 return null;
const tail = () => { };
return CLU.lastNode; // 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 finalStr = finalStr + " null";
const at = (index) => { return finalStr;
// set to first node };
const MCP = { // inserts a new node with the provided value at the given index
currentNode: CLU.firstNode const insertAt = (value, index) => {
} if (index === 0) {
// loop through LL to index prepend(value);
for (let i = 0; i < index; i++) { return;
MCP.currentNode = MCP.currentNode.nextNode;
}
return MCP.currentNode;
} }
// removes the last element from the list if (index === CLU.totalNodes) {
const pop = () => { append(value);
// gets second to last node return;
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 true if the passed in value is in the list and otherwise returns false. if (index === CLU.totalNodes + 1) {
const contains = (value) => { console.log("Your index value is too high!");
find(value) ? true : false; return;
} }
// returns the index of the node containing value, or null if not found. !!!!!!!!!!! // gets node at previous index
const find = (value) => { const prevNode = at(index - 1);
// set to first node // gets node at current index
const MCP = { const currNode = at(index);
currentNode: CLU.firstNode // makes new node, sets current node at index as next
} const newNode = Node(value, currNode);
// for all nodes // sets itself as previous index node's next node
for (let i = 0; i < CLU.totalNodes; i++) { prevNode.nextNode = newNode;
// if node value matches, set to true // updates counter
if (MCP.currentNode.value === value) { CLU.totalNodes = CLU.totalNodes + 1;
return i; };
} // removes the node at the given index
// otherwise set to next node const removeAt = (index) => {
MCP.currentNode = MCP.currentNode.nextNode; // pops if final value
} if (index === CLU.totalNodes - 1) {
return null; pop();
return;
} }
// print Nodes out and preview them in the console if (index === 0) {
const toString = () => { CLU.firstNode = CLU.firstNode.nextNode;
// final String value CLU.totalNodes = CLU.totalNodes - 1;
let finalStr = ''; return;
// 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;
} }
// inserts a new node with the provided value at the given index // gets node at previous index
const insertAt = (value, index) => { const prevNode = at(index - 1);
if (index === 0) { // sets its next node to the one ahead of the current one, effectively removing it from the chain
prepend(value); prevNode.nextNode = at(index + 1);
return; // removes a digit from the counter
} CLU.totalNodes = CLU.totalNodes - 1;
if (index === CLU.totalNodes) { // catches the offchance
append(value); if (CLU.totalNodes === 0) {
return; CLU.firstNode = CLU.lastNode = null;
}
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;
} }
// removes the node at the given index };
const removeAt = (index) => { return {
// pops if final value append,
if (index === CLU.totalNodes - 1) { prepend,
pop(); size,
return; head,
} tail,
if (index === 0) { at,
CLU.firstNode = CLU.firstNode.nextNode; pop,
CLU.totalNodes = CLU.totalNodes - 1; contains,
return; find,
} toString,
// gets node at previous index insertAt,
const prevNode = at(index - 1); removeAt,
// 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
}
}

View file

@ -1,7 +1,7 @@
// node factory // node factory
export const Node = (value = null, nextNode = null) => { export const Node = (value = null, nextNode = null) => {
return { return {
value, value,
nextNode nextNode,
} };
} };