Created
August 7, 2016 20:34
-
-
Save ChrisLTD/2e271bbb82c1a54246830b56e35fc107 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
var head = null; // head of linked list | |
function addNode(val) { | |
var node = { | |
val: val, | |
next: null | |
} | |
if (head == null) { | |
head = node; | |
} else { | |
var lastNode = getLastNode(); | |
lastNode.next = node; | |
} | |
} | |
function popNode() { | |
if (head == null) { | |
console.log("list is empty"); | |
return null; | |
} | |
var pop = null; | |
if (head.next == null) { | |
pop = head; | |
head = null; | |
return pop; | |
} | |
var walker = head; | |
while (walker.next != null) { | |
if (walker.next.next == null) { | |
pop = walker.next; | |
walker.next = null; | |
} else { | |
walker = walker.next; | |
} | |
} | |
return pop; | |
} | |
function getLastNode() { | |
if (head == null) { | |
console.log("list is empty"); | |
return null; | |
} | |
var walker = head; | |
while (walker.next != null) { | |
walker = walker.next; | |
} | |
return walker; | |
} | |
function printList() { | |
if (head == null) { | |
console.log("list is empty"); | |
return null; | |
} | |
var walker = head; | |
while (walker.next != null) { | |
console.log(walker.val); | |
walker = walker.next; | |
} | |
console.log(walker.val); | |
} | |
function getNodeAtPosition(position) { | |
if (head == null) { | |
console.log("list is empty"); | |
return null; | |
} | |
var walker = head; | |
for (var i = 0; i < position; i += 1) { | |
if (walker.next != null) { | |
walker = walker.next; | |
} else { | |
console.log("List isn't " + position + " positions long." ); | |
return null; | |
} | |
} | |
return walker; | |
} | |
function addNodeAtPosition(val, position) { | |
var node = { | |
val: val, | |
next: null | |
} | |
var walker = head; | |
if (position == 0) { | |
node.next = walker; | |
head = node; | |
} else { | |
for (var i = 0; i < (position - 1); i += 1) { | |
if (walker.next != null) { | |
walker = walker.next; | |
} else { | |
console.log("List isn't " + position + " positions long." ); | |
return null; | |
} | |
} | |
node.next = walker.next; | |
walker.next = node; | |
} | |
} | |
function removeNodeAtPosition(position) { | |
var walker = head; | |
if (position == 0) { | |
head = head.next; | |
} else { | |
var prevNode = getNodeAtPosition(position - 1); | |
var nextNode = getNodeAtPosition(position + 1); | |
if (nextNode) { | |
prevNode.next = nextNode; | |
} else { | |
prevNode.next = null; | |
} | |
} | |
} | |
function reverseList() { | |
var tempHead = popNode(); | |
var walker = tempHead; | |
var prevNode = popNode(); | |
while (prevNode != null) { | |
walker.next = prevNode; | |
walker = prevNode; | |
prevNode = popNode(); | |
} | |
head = tempHead; | |
} | |
// var test add node and last node | |
head = { val: "1", next: { val: "2", next: { val: "3", next: null } } }; | |
addNode('4'); | |
//console.log(head); | |
printList(); | |
// test find position | |
console.log(getNodeAtPosition(2)); | |
console.log(getNodeAtPosition(4)); | |
// test add node at position | |
addNodeAtPosition('2a', 2); | |
addNodeAtPosition('0', 0); | |
printList(); | |
// test removeNodeAtPosition | |
removeNodeAtPosition(0); | |
printList(); | |
removeNodeAtPosition(2); | |
removeNodeAtPosition(3); | |
printList(); | |
// test popNode | |
console.log(popNode()); | |
console.log(popNode()); | |
console.log(popNode()); | |
printList(); | |
// test reverseList | |
addNode('1'); | |
addNode('2'); | |
addNode('3'); | |
addNode('4'); | |
addNode('5'); | |
addNode('6'); | |
reverseList(); | |
printList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment