Skip to content

lab-10-chris #14

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 2 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-10-chris/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;
};
18 changes: 18 additions & 0 deletions lab-10-chris/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lab-10-chris",
"version": "1.0.0",
"description": "Single linked list prototypes",
"main": "''",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"author": "Chris",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.3.0"
}
}
148 changes: 148 additions & 0 deletions lab-10-chris/sll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
'use strict';

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

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

// O(1)
SLL.prototype.prepend = function(value) {
let node = new Node(value);

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

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

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

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

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

function _setLastNode(node) {
Copy link
Contributor

Choose a reason for hiding this comment

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

recursion hurts my brain

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

// O(n)
SLL.prototype.reverse = function() {

if(!this.head || !this.head.next) return this;

let nodes = [],
currentNode = this.head,
node,
reverseList = new SLL();

while(currentNode) {
nodes.push(currentNode);
currentNode = currentNode.next;
}

reverseList.head = nodes.pop();
currentNode = reverseList.head;

node = nodes.pop();

while(node) {
node.next = null;
currentNode.next = node;
currentNode = currentNode.next;
node = nodes.pop();
}

return reverseList;
};

// O(n)
SLL.prototype.remove = function(index) {
let currentNode = this.head,
i=0, previous;

//if list is empty, exit out
if(!currentNode) return;

//Check if first node
if(index === 0){
this.head = currentNode.next;
}else{

while(i < index){
previous = currentNode;
currentNode = currentNode.next;
i++;
}

previous.next = currentNode.next;

return previous;
}
/*
// pass in the index you want to remove
let list = a list with many nodes

list.remove(some index)
*/
};

// O(n) this counts from the end
SLL.prototype.nthNode = function(n) {
let currentNode = this.head,
i = 1,
nthNode;

if(n <= 0) return;

while(currentNode) {
if(i === n) {
nthNode = this.head;
} else if (i - n > 0) {
nthNode = nthNode.next;
}
i++;

currentNode = currentNode.next;
}
return nthNode;
};

// O(n)
SLL.prototype.findMiddle = function(err) {
if(err) console.error(err);
let middleEnd = [];

let i = 0,
currentNode = this.head,
middleNode = this.head;

while(currentNode.next != null){
i++;
if(i % 2 ==0){
middleNode = middleNode.next;
}
currentNode = currentNode.next;
}

if(i % 2 == 1){
middleNode = middleNode.next;
}
middleEnd.push(middleNode, i);
return middleEnd;
};
103 changes: 103 additions & 0 deletions lab-10-chris/test/SLL-proto-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'use strict';

const SLL = require('../sll');
const expect = require('chai').expect;

describe('SLL.prototypes', function() {

describe('.reverse()', function() {
let list = new SLL();
list.append(1); list.append(2); list.append(3);
list.append(4); list.append(5);
let newList = list.reverse();

it('should return a new list', () => {
expect(newList).to.exist;
});

it('should reverse the order of the list', () => {
expect(newList.head.val).to.equal(5);
});

it('should make the previous head.next null', () => {
expect(newList.head.next.next.next.next.val).to.deep.equal(1);
expect(newList.head.next.next.next.next.next).to.deep.equal(null);
});
});

describe('.findMiddle()', function() {
let list = new SLL();
list.append(1); list.append(2); list.append(3);
list.append(4); list.append(5);

it('should return the middle node in an odd list', () => {
let middle = list.findMiddle();
let mid = middle[0];
// if list is odd it will return the middle
expect(mid.val).to.equal(3);
});

it('should return the greater middle node in an even list', () => {
list.append(6);
let middle = list.findMiddle();
let mid = middle[0];

expect(mid.val).to.equal(4);
});

it('should return the index of the last node', () => {
let middle = list.findMiddle();
let midIndex = middle[1];

expect(midIndex).to.equal(5);
});
});

describe('.nthNode()', function() {
let list = new SLL();
list.append(1); list.append(2); list.append(3);
list.append(4); list.append(5);

it('should return the last node', () => {
let findLast = list.nthNode(1);

expect(findLast.next).to.equal(null);
});

it('should return the second to the last node', () => {
let find = list.nthNode(2);

expect(find.next.next).to.equal(null);
});

it('should return list head node', () => {
let find = list.nthNode(5);

expect(find).to.equal(list.head);
});
});

describe('.remove()', function() {
let list = new SLL();
list.append(1); list.append(2); list.append(3);
list.append(4); list.append(5);

it('should remove the head', () => {
list.remove(0);

expect(list.head.val).to.deep.equal(2);
});

it('should remove the last node', () => {
list.remove(3);

expect(list.head.next.next.next).to.deep.equal(null);
});

it('should have only two nodes left', () => {
list.remove(1);

expect(list.head.next.next).to.deep.equal(null);
});
});
});