Skip to content

Lab-10-Lindsay #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lab-lindsay/model/list-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(value) {
this.val = value;
this.next = null;
};
27 changes: 27 additions & 0 deletions lab-lindsay/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "lab-10-lindsay",
"version": "1.0.0",
"description": "lab-10-linked-list-lindsay",
"main": "sll.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lindsgil/lab-10-career-development.git"
},
"keywords": [
"lab-10",
"linked-list",
"lindsay"
],
"author": "lindsay",
"license": "MIT",
"bugs": {
"url": "https://github.com/lindsgil/lab-10-career-development/issues"
},
"homepage": "https://github.com/lindsgil/lab-10-career-development#readme",
"devDependencies": {
"mocha": "^3.3.0"
}
}
110 changes: 110 additions & 0 deletions lab-lindsay/sll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';

const Node = require('./model/list-node');

//[val, next];
//(HEAD) [val, next] This list is empty so point the head at the new Node

//(HEAD) [val, next] => [val, next] => [val, next] => [val, next] => [val, next]

//Prepend() adds a new node at the HEAD append() adds a new node after the last Node

const SLL = module.exports = function() {
this.head = null;
this._length = 0;
};

SLL.prototype.prepend = function(value) {
let node = new Node(value);
if(!this.head) {
this.head = node;
this._length++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping track of the length is a good idea. im curious as to why you decided to implement this? for me, it'd be nice to have that info so after a removal i could test the length property :)

return this;
}
node.next = this.head;
this.head = node;
this._length++;
return this;
};

SLL.prototype.append = function(value) {
let node = new Node(value);
let lastNode = null;

if(!this.head) {
this.head = node;
this._length++;
return this;
}

_setLastNode(this.head);
lastNode.next = node;
return this;

function _setLastNode(node) {
if(!node) return;
lastNode = node;
_setLastNode(node.next);
}
};

SLL.prototype.searchNodeAt = function(position) {
let currentNode = this.head,
length = this._length,
count = 1,
message = {failure: 'Failure: Non existent node at that position in this list'};

if(length ==== 0 || position < 1 || position > length) {
throw new Error(message.failure);
}

while(count < position) {
currentNode = currentNode.next;
count++;
}
return currentNode;
};

SLL.prototype.remove = function(position) {
let currentNode = this.head,
length = this._length,
count = 0,
beforeNodeToDelete = null,
nodeToDelete = null,
deletedNode = null;

if(position < 0 || position > length) {
throw new Error(message.failure);
}

if(position === 1) {
this.head = currentNode.next;
deletedNode = currentNode;
currentNode = null;
this._length--;

return deletedNode;
}

while(count < position) {
beforeNodeToDelete = currentNode;
nodeToDelete = currentNode.next;
count++;
}
beforeNodeToDelete.next = nodeToDelete.next;
deletedNode = nodeToDelete;
nodeToDelete = null;
this._length--;

return deletedNode;
};

SLL.prototype.reverse = function(value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean. i like it!

let newSLL = new SLL();

while(this.head) {
newSLL.prepend(this.head.value);
this.head = this.remove(this.head);
}
return newSLL;
};
13 changes: 13 additions & 0 deletions lab-lindsay/test/sll-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// describe('data-structures', function() {
// describe('SLL-reverse-Method', function() {
// it('should iterate through the List array and push to newListArray with the same values', function(){
// let list = new List(3, 4, 8);
// let newListArray = [];
// list.forEach(function(val){
// newListArray.push(val);
// });
// console.log(newListArray);
// expect(newListArray).to.deep.equal([ 3, 4, 8 ]);
// });
// });
// });