Last active
May 23, 2018 12:07
-
-
Save osazemeu/b370e244dc807cc329c4acd55757b5c4 to your computer and use it in GitHub Desktop.
JS Heaps and Stacks
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
//Javascript stack linked list data structure (no array) | |
function node(value, noderef) { | |
this.value = value; | |
this.next = noderef; | |
} | |
function stack() { | |
this.push = function (value) { | |
this.next = this.first; | |
this.first = new node(value, this.next); | |
} | |
this.pop = function () { | |
var popvalue = this.first.value; | |
this.first = this.first.next; | |
return popvalue; | |
} | |
this.hasnext = function () { | |
return this.next != undefined; | |
} | |
this.isempty = function () { | |
return this.first == undefined; | |
} | |
} | |
//Javascript stack linked list data structure (no array) | |
function node(value, noderef) { | |
this.value = value; | |
this.next = undefined; | |
} | |
function queue() { | |
this.enqueue = function (value) { | |
this.oldlast = this.last; | |
this.last = new node(value); | |
if (this.isempty()) | |
this.first = this.last; | |
else | |
this.oldlast.next = this.last; | |
} | |
this.dequeue = function () { | |
var queuvalue = this.first.value; | |
this.first = this.first.next; | |
return queuvalue; | |
} | |
this.hasnext = function () { | |
return this.first.next != undefined; | |
} | |
this.isempty = function () { | |
return this.first == undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment