Last active
December 28, 2015 01:39
-
-
Save marijer/7422588 to your computer and use it in GitHub Desktop.
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
/* | |
ASSIGNMENT | |
Implement a stack data structure as a JavaScript object that contains the following methods: | |
push(data) - Adds a data element to the stack | |
The parameter is the data element to add to the stack. | |
pop(callback) - Removes a data element from the stack | |
The parameter to pop is a callback function that should expect two parameters - err and value. The callback's first param, err, is set to null if the pop operation was successful or "Underflow" if the stack was empty when called. The callback's second parameter, value, is the value removed from the stack. | |
*/ | |
var LinkedList = function() { | |
this.head = null; | |
} | |
var newNode = function( data ) { | |
this.data = data; | |
this.prev = null; | |
} | |
LinkedList.prototype.push = function ( data ) { | |
var node = new newNode ( data ); | |
node.prev = this.head || null; | |
this.head = node; | |
} | |
LinkedList.prototype.pop = function () { | |
if ( this.head === null && this.head.prev === undefined ) { return "nothing there" } | |
this.head = this.head.prev; | |
} | |
// test out the application | |
var ll = new LinkedList(); | |
// insert new | |
ll.push ( '1. hi' ); | |
ll.push ( '2. hi' ); | |
ll.push ( '3. hi' ); | |
console.log ( ll.head ); | |
ll.pop (); | |
ll.pop (); | |
console.log ( ll.head ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment