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
Linked list implementation in ES6

View file

@ -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
}
}
};
return {
append,
prepend,
size,
head,
tail,
at,
pop,
contains,
find,
toString,
insertAt,
removeAt,
};
};

View file

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