linted and prettiered
This commit is contained in:
parent
ffa48c45b0
commit
451e07a21b
5 changed files with 177 additions and 171 deletions
10
.eslintrc.cjs
Normal file
10
.eslintrc.cjs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module.exports = {
|
||||
"extends": [
|
||||
"eslint-config-prettier",
|
||||
"prettier"
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module",
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
module.exports = {
|
||||
"extends": [
|
||||
"eslint-config-prettier"
|
||||
]
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
# js-linkedlist
|
||||
|
||||
Linked list implementation in ES6
|
||||
|
|
@ -5,8 +5,8 @@ export const LinkedList = () => {
|
|||
const CLU = {
|
||||
firstNode: null,
|
||||
lastNode: null,
|
||||
totalNodes: 0
|
||||
}
|
||||
totalNodes: 0,
|
||||
};
|
||||
// adds a new node containing value to the end of the list
|
||||
const append = (value) => {
|
||||
// make node
|
||||
|
|
@ -20,7 +20,7 @@ export const LinkedList = () => {
|
|||
// 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
|
||||
|
|
@ -36,31 +36,31 @@ export const LinkedList = () => {
|
|||
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
|
||||
}
|
||||
currentNode: CLU.firstNode,
|
||||
};
|
||||
// loop through LL to index
|
||||
for (let i = 0; i < index; i++) {
|
||||
MCP.currentNode = MCP.currentNode.nextNode;
|
||||
}
|
||||
return MCP.currentNode;
|
||||
}
|
||||
};
|
||||
// removes the last element from the list
|
||||
const pop = () => {
|
||||
// gets second to last node
|
||||
|
|
@ -73,17 +73,17 @@ export const LinkedList = () => {
|
|||
if (CLU.totalNodes === 0) {
|
||||
CLU.firstNode = CLU.lastNode = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
// 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
|
||||
}
|
||||
currentNode: CLU.firstNode,
|
||||
};
|
||||
// for all nodes
|
||||
for (let i = 0; i < CLU.totalNodes; i++) {
|
||||
// if node value matches, set to true
|
||||
|
|
@ -94,15 +94,15 @@ export const LinkedList = () => {
|
|||
MCP.currentNode = MCP.currentNode.nextNode;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
// print Nodes out and preview them in the console
|
||||
const toString = () => {
|
||||
// final String value
|
||||
let finalStr = '';
|
||||
let finalStr = "";
|
||||
// set to first node
|
||||
const MCP = {
|
||||
currentNode: CLU.firstNode
|
||||
}
|
||||
currentNode: CLU.firstNode,
|
||||
};
|
||||
// loop through LL to index
|
||||
for (let i = 0; i < CLU.totalNodes; i++) {
|
||||
finalStr = finalStr + ` ( ${MCP.currentNode.value} ) ->`;
|
||||
|
|
@ -110,7 +110,7 @@ export const LinkedList = () => {
|
|||
}
|
||||
finalStr = finalStr + " null";
|
||||
return finalStr;
|
||||
}
|
||||
};
|
||||
// inserts a new node with the provided value at the given index
|
||||
const insertAt = (value, index) => {
|
||||
if (index === 0) {
|
||||
|
|
@ -135,7 +135,7 @@ export const LinkedList = () => {
|
|||
prevNode.nextNode = newNode;
|
||||
// updates counter
|
||||
CLU.totalNodes = CLU.totalNodes + 1;
|
||||
}
|
||||
};
|
||||
// removes the node at the given index
|
||||
const removeAt = (index) => {
|
||||
// pops if final value
|
||||
|
|
@ -158,7 +158,7 @@ export const LinkedList = () => {
|
|||
if (CLU.totalNodes === 0) {
|
||||
CLU.firstNode = CLU.lastNode = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
return {
|
||||
append,
|
||||
prepend,
|
||||
|
|
@ -171,6 +171,6 @@ export const LinkedList = () => {
|
|||
find,
|
||||
toString,
|
||||
insertAt,
|
||||
removeAt
|
||||
}
|
||||
}
|
||||
removeAt,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
export const Node = (value = null, nextNode = null) => {
|
||||
return {
|
||||
value,
|
||||
nextNode
|
||||
}
|
||||
}
|
||||
nextNode,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue