-
-
Save codecademydev/9752087f87a2dc26009fdb9c58928a15 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Node = require("./Node"); | |
const LinkedList = require("./LinkedList"); | |
const myList = new LinkedList(); | |
myList.addToHead("Node 1"); | |
myList.addToHead("Node 2"); | |
myList.addToHead("Node 3"); | |
myList.addToHead("Node 4"); | |
// Add checkpoint 2 code below: | |
const myNodeRecursive = myList.findNodeIteratively("Node 2"); | |
console.log(myNodeRecursive); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Node = require("./Node"); | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
} | |
addToHead(data) { | |
const nextNode = new Node(data); | |
const currentHead = this.head; | |
this.head = nextNode; | |
if (currentHead) { | |
this.head.setNextNode(currentHead); | |
} | |
} | |
addToTail(data) { | |
let lastNode = this.head; | |
if (!lastNode) { | |
this.head = new Node(data); | |
} else { | |
let temp = this.head; | |
while (temp.getNextNode() !== null) { | |
temp = temp.getNextNode(); | |
} | |
temp.setNextNode(new Node(data)); | |
} | |
} | |
removeHead() { | |
const removedHead = this.head; | |
if (!removedHead) { | |
return; | |
} | |
if (removedHead.next) { | |
this.head = removedHead.next; | |
} | |
return removedHead.data; | |
} | |
printList() { | |
let currentNode = this.head; | |
let output = "<head> "; | |
while (currentNode !== null) { | |
output += currentNode.data + " "; | |
currentNode = currentNode.next; | |
} | |
output = output.concat("<tail>"); | |
console.log(output); | |
} | |
findNodeIteratively(data) { | |
let currentNode = this.head; | |
while (currentNode !== null) { | |
if (currentNode.data === data) { | |
return currentNode; | |
} | |
currentNode = currentNode.next; | |
} | |
return null; | |
} | |
findNodeRecursively(data, currentNode = this.head) { | |
if (currentNode === null) { | |
return null; | |
} else if (currentNode.data === data) { | |
return currentNode; | |
} else { | |
return this.findNodeRecursively(data, currentNode.next); | |
} | |
} | |
} | |
module.exports = LinkedList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data) { | |
this.data = data; | |
this.next = null; | |
} | |
setNextNode(node) { | |
if (!(node instanceof Node)) { | |
throw new Error("Next node must be a member of the Node class"); | |
} | |
this.next = node; | |
} | |
setNext(data) { | |
this.next = data; | |
} | |
getNextNode() { | |
return this.next; | |
} | |
} | |
module.exports = Node; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment