Created
May 1, 2019 23:15
-
-
Save jdaly13/7a8524775c043e7ca5787d1ac3c09300 to your computer and use it in GitHub Desktop.
// source https://jsbin.com
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function Node (val) { | |
this.value = val; | |
this.next = null; | |
} | |
function LinkedList (val = 42) { | |
var node = new Node(val); | |
this.head = node; | |
this.tail = node; | |
this.length = 1; | |
} | |
LinkedList.prototype.push = function (val) { | |
var newTail = new Node(val); | |
this.tail.next = newTail; | |
this.tail = newTail; | |
this.length++; | |
return newTail; | |
} | |
LinkedList.prototype.pop = function (node) { | |
if (!this.length) return null; | |
var nodeToRemove = this.tail | |
if (this.head === this.tail) { | |
this.head = null; | |
this.tail = null; | |
this.length--; | |
return nodeToRemove | |
} | |
var currentNode = this.head; | |
var secondToLast; | |
while(currentNode) { | |
if (currentNode.next === this.tail) { | |
secondToLast = currentNode; | |
break; | |
} | |
currentNode = currentNode.next; | |
} | |
secondToLast.next = null; | |
// Move the tail to the second to last node | |
this.tail = secondToLast; | |
this.length--; | |
// Initialized to this.tail | |
return nodeToRemove; | |
} | |
LinkedList.prototype.unshift = function (val) { | |
var newHead = new Node(val); | |
if (this.head === this.tail) { | |
this.head = newHead; | |
this.tail = newHead; | |
this.length = 1; | |
return this.head; | |
} | |
var oldHead = this.head; | |
this.head = newHead; | |
newHead.next = oldHead; | |
this.length++; | |
return this.head; | |
} | |
LinkedList.prototype.shift = function () { | |
var oldHead = this.head; | |
var newHead = this.head.next; | |
this.head = newHead; | |
oldHead.next = null; | |
} | |
var myFirstList = new LinkedList('fudge'); | |
myFirstList.push('lollipop'); | |
myFirstList.push('cotton candy'); | |
myFirstList.push('fish') | |
myFirstList.pop(); | |
myFirstList.unshift('pop tarts'); | |
console.log(myFirstList); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function Node (val) { | |
this.value = val; | |
this.next = null; | |
} | |
function LinkedList (val = 42) { | |
var node = new Node(val); | |
this.head = node; | |
this.tail = node; | |
this.length = 1; | |
} | |
LinkedList.prototype.push = function (val) { | |
var newTail = new Node(val); | |
this.tail.next = newTail; | |
this.tail = newTail; | |
this.length++; | |
return newTail; | |
} | |
LinkedList.prototype.pop = function (node) { | |
if (!this.length) return null; | |
var nodeToRemove = this.tail | |
if (this.head === this.tail) { | |
this.head = null; | |
this.tail = null; | |
this.length--; | |
return nodeToRemove | |
} | |
var currentNode = this.head; | |
var secondToLast; | |
while(currentNode) { | |
if (currentNode.next === this.tail) { | |
secondToLast = currentNode; | |
break; | |
} | |
currentNode = currentNode.next; | |
} | |
secondToLast.next = null; | |
// Move the tail to the second to last node | |
this.tail = secondToLast; | |
this.length--; | |
// Initialized to this.tail | |
return nodeToRemove; | |
} | |
LinkedList.prototype.unshift = function (val) { | |
var newHead = new Node(val); | |
if (this.head === this.tail) { | |
this.head = newHead; | |
this.tail = newHead; | |
this.length = 1; | |
return this.head; | |
} | |
var oldHead = this.head; | |
this.head = newHead; | |
newHead.next = oldHead; | |
this.length++; | |
return this.head; | |
} | |
LinkedList.prototype.shift = function () { | |
var oldHead = this.head; | |
var newHead = this.head.next; | |
this.head = newHead; | |
oldHead.next = null; | |
} | |
var myFirstList = new LinkedList('fudge'); | |
myFirstList.push('lollipop'); | |
myFirstList.push('cotton candy'); | |
myFirstList.push('fish') | |
myFirstList.pop(); | |
myFirstList.unshift('pop tarts'); | |
console.log(myFirstList); | |
</script></body> | |
</html> |
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
function Node (val) { | |
this.value = val; | |
this.next = null; | |
} | |
function LinkedList (val = 42) { | |
var node = new Node(val); | |
this.head = node; | |
this.tail = node; | |
this.length = 1; | |
} | |
LinkedList.prototype.push = function (val) { | |
var newTail = new Node(val); | |
this.tail.next = newTail; | |
this.tail = newTail; | |
this.length++; | |
return newTail; | |
} | |
LinkedList.prototype.pop = function (node) { | |
if (!this.length) return null; | |
var nodeToRemove = this.tail | |
if (this.head === this.tail) { | |
this.head = null; | |
this.tail = null; | |
this.length--; | |
return nodeToRemove | |
} | |
var currentNode = this.head; | |
var secondToLast; | |
while(currentNode) { | |
if (currentNode.next === this.tail) { | |
secondToLast = currentNode; | |
break; | |
} | |
currentNode = currentNode.next; | |
} | |
secondToLast.next = null; | |
// Move the tail to the second to last node | |
this.tail = secondToLast; | |
this.length--; | |
// Initialized to this.tail | |
return nodeToRemove; | |
} | |
LinkedList.prototype.unshift = function (val) { | |
var newHead = new Node(val); | |
if (this.head === this.tail) { | |
this.head = newHead; | |
this.tail = newHead; | |
this.length = 1; | |
return this.head; | |
} | |
var oldHead = this.head; | |
this.head = newHead; | |
newHead.next = oldHead; | |
this.length++; | |
return this.head; | |
} | |
LinkedList.prototype.shift = function () { | |
var oldHead = this.head; | |
var newHead = this.head.next; | |
this.head = newHead; | |
oldHead.next = null; | |
} | |
var myFirstList = new LinkedList('fudge'); | |
myFirstList.push('lollipop'); | |
myFirstList.push('cotton candy'); | |
myFirstList.push('fish') | |
myFirstList.pop(); | |
myFirstList.unshift('pop tarts'); | |
console.log(myFirstList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment