Last active
December 27, 2015 23:39
-
-
Save marijer/7407343 to your computer and use it in GitHub Desktop.
Linked List
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
// HOMEWORK WEEK 3, day 1 - Linked list | |
/* | |
prepend(data) - insert a new node with the value data at the beginning of the list | |
append(data) - insert a new node with the value data at the end of the list | |
pop_front(callback) - removes the first element of the list and invokes callback with the data value passed as the parameter to callback | |
pop_back(callback) - removes the last element of the list and invokes callback with the data value passed as the parameter to callback | |
*/ | |
var LinkedList = function() { | |
this.head = null; | |
} | |
var newNode = function(data) { | |
this.data = data; | |
this.prev = null; | |
} | |
LinkedList.prototype.append = function(data) { | |
var node = new newNode(data); | |
if (this.head === null) { | |
this.head = node; | |
this.prev = null; | |
} | |
else { | |
var temp = this.head; | |
while (temp.next !== null) { | |
temp = temp.next; | |
} | |
temp.next = node; | |
} | |
this.numItems++ | |
} | |
LinkedList.prototype.prepend = function ( data ) { | |
var node = new newNode( data ); | |
// sets next node to the head; | |
node.next = this.head; | |
// FIX - get circular --- this.head.prev = node; | |
// sets new node as head; | |
this.head = node; | |
} | |
LinkedList.prototype.pop_front = function ( callback ) { | |
// cache first element | |
var popped = this.head; | |
if( !popped ) return callback ("stack is empty"); | |
// set child to head | |
this.head = this.head.next; | |
// return first element in callback | |
callback ( null, popped ); | |
} | |
LinkedList.prototype.pop_back = function ( callback ) { | |
var temp = this.head || undefined; | |
if( !temp ) return callback ("stack is empty"); | |
while( temp.next.next !== null ) { | |
temp = temp.next; | |
} | |
var popped = temp.next; | |
temp.next = null; | |
callback ( null, popped ); | |
} | |
var test = function( err, el ) { | |
if( err ) { | |
console.log (err); | |
} else { | |
console.log ( el.data + ' has been removed;') | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// TEST CODE | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// creates a new double linked list | |
var dLL = new LinkedList(); | |
dLL.pop_back(test); | |
dLL.append("hello"); | |
dLL.append("class"); | |
dLL.append("world"); | |
dLL.append("try"); | |
// insert a new node with the value data at the beginning of the list | |
dLL.prepend("first"); | |
// removes the first element of the list and invokes callback with the data value passed as the parameter to callback | |
dLL.pop_front (test); | |
dLL.pop_back(test); | |
console.log(dLL.head); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment