Skip to content

Reverse and remove are working, all tests for reverse are working. #13

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
Empty file added index.js
Empty file.
6 changes: 6 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "lab-10-career-development",
"version": "1.0.0",
"description": "![cf](http://i.imgur.com/7v5ASc8.png) lab 10 - Linked List Data Structure\r ====",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/renegadellama/lab-10-career-development.git"
},
"author": "Isak Swearingen",
"license": "MIT",
"bugs": {
"url": "https://github.com/renegadellama/lab-10-career-development/issues"
},
"homepage": "https://github.com/renegadellama/lab-10-career-development#readme",
"devDependencies": {
"chai": "^3.5.0",
"chai-http": "^3.0.0",
"mocha": "^3.3.0"
}
}
97 changes: 97 additions & 0 deletions sll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

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

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

SLL.prototype.print = function () {
let output = '[';
let current = this.head;

while (current !== null) {
output += current.val;
if(current.next !== null) {
output += ',';
}
current = current.next;
}

output += ']';
console.log(output);
};

SLL.prototype.remove = function(val) {
if(this.head.val === val) {
this.head = this.head.next;
return;
}

let prev = null;
let node = this.head;
while (node.val !== val){
prev = node;
node = node.next;
}
prev.next = node.next;
this.end = node.next;
}

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;
};

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) {
if(!node) return;
lastNode = node;
_setLastNode(node.next);
}
};


SLL.prototype.reverse = function () {
let previous = null;
let current = this.head;
let next;

while(current) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
this.head = previous;
};

let ll = new SLL();
ll.append(5);
ll.append(10);
ll.append(15);
ll.append(20);
ll.append(25);
ll.prepend(30);

ll.reverse();
ll.remove(10);
ll.print();
65 changes: 65 additions & 0 deletions test/sll-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';
const SLL = require('../sll');
const list = require('../list-node')
const expect = require('chai').expect;

let llOne = new SLL();
llOne.append(5);
llOne.append(10);
llOne.append(15);
llOne.append(20);
llOne.print();

let llTwo = new SLL();
llTwo.append(20);
llTwo.append(15);
llTwo.append(10);
llTwo.append(5);
llTwo.print();

let llThree = new SLL();
llThree.append(10);
llThree.append(15);
llThree.append(20);
llThree.print();

let llOneTest = llOne.reverse();
let llTwoTest = llTwo.print();
// llOne.remove(5);
// let llOneRemoved = llOne.remove(5);
// console.log('removed ', llOneRemoved);
console.log(llOneTest);
console.log(llTwoTest);


describe('Testing SLL methods', function() {
describe('reverse functionality', function() {
it('Should reverse the values of the list when reversed', done => {
expect(llOneTest).to.equal(llTwoTest);
done();
});
it('Should have the head value equal the tail value once it is reversed.', done => {
expect(llOne.head.val).to.equal(llTwo.head.val);
done();
});
it('Should have the same second node values', done => {
expect(llOne.head.next.val).to.equal(llTwo.head.next.val);
done();
});
});
describe('remove functionality', function() {
describe('it should remove a value when the funtion is called', done => {
let llOne = new SLL();
llOne.append(5);
llOne.append(10);
llOne.append(15);
llOne.append(20);
llOne.print();
llOne.remove(5);
console.log('oneVAL ', llOne.head.val);
console.log('twoVAL ',llThree.head.val);
expect(llOne.head.val).to.equal(llThree.head.val);
done();
});
});
});