Skip to content

Steven johnson #4

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 5 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
25 changes: 25 additions & 0 deletions lab-steven/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"rules": {
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"no-console": "off",
"indent": [ "error", 2 ],
"semi": ["error", "always"]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"globals": {
"__API_URI__": false,
"__DEBUG__": false
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
65 changes: 65 additions & 0 deletions lab-steven/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Created by https://www.gitignore.io/api/osx,linux,node,vim

### OSX ###
.DS_Store
.AppleDouble
.LSOverride

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*


### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

### Vim ###
# swap
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# session
Session.vim
# temporary
.netrwhist
*~

# auto-generated tag files
tags
77 changes: 77 additions & 0 deletions lab-steven/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const Node = require('./lib/nodeCreator.js');

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

//O(n)
SLL.prototype.prepend = function(val){
let node = new Node(val);
if(!this.head){
this.head = node;
return this;
}

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

//O(n)
SLL.prototype.append = function(val){
let node = new Node(val);
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);
}
};

//O(n)
SLL.prototype.removeNode = function(node){
let prevNode = this.head;

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

_findPrev(this.head);
prevNode.next = node.next;
return this;

function _findPrev(pNode){ //check each node from head to see if pNode.next === node
if (pNode.next === node) return;
prevNode = pNode.next;
_findPrev(pNode.next);
}
};

//O(n)
SLL.prototype.reverse = function(){
let currNode = this.head;
let prevNode = null;
let nextNode = null;

while(currNode !== null){
nextNode = currNode.next;
currNode.next = prevNode;
prevNode = currNode;
currNode = nextNode;
}
this.head = prevNode;
return this;
};
6 changes: 6 additions & 0 deletions lab-steven/lib/nodeCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(val){
this.value = val;
this.next = null;
};
21 changes: 21 additions & 0 deletions lab-steven/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "lab-steven",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"lint": "eslint ../"
},
"keywords": [
"singly",
"linked",
"list"
],
"author": "CF Steven Johnson",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.3.0"
}
}
17 changes: 17 additions & 0 deletions lab-steven/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Lab 10 - Singly Linked Lists
This is a lab assignment in CF401 where we create a singly linked list object, and add different prototype methods to the SLL constructor.

### Setup
- Clone this repo
- Run ```npm install``` in your terminal (make sure you're at the lab-steven filepath in the repo)

### Methods
- ```new SLL()``` : creates a Singly linked list
- ```SLL.prepend(value)``` : prepends a new node as new Head of SLL with designated value
- ```SLL.append(vale)``` : appends a new node the end of SLL with designated value
- ```SLL.removeNode(node)``` : removes designated node from SLL
- ```SLL.reverse()``` : reverses order of SLL

### Scripts
- ```npm run lint``` : lints the code
- ```npm run test``` : runs tests (using mocha and chai, which are devDependencies)
53 changes: 53 additions & 0 deletions lab-steven/test/sll-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

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

describe('index.js', function(){
let sll;
beforeEach(function(){
sll = new SLL();
sll.prepend(1);
sll.prepend(2);
sll.prepend(3);
});
afterEach(function(){
sll = null;
});

describe('#SLL.prepend', function(){
it('inserts the node before existing head node', function(done){
sll.prepend(10);
expect(sll.head.value).to.equal(10);
done();
});
});

describe('#SLL.append', function(){
it('inserts node at end of SLL', function(done){
sll.append(11);
expect(sll.head.next.next.next.value).to.equal(11);
done();
});
});

describe('#SLL.removeNode', function(){
it('removes the specified node', function(done){
sll.removeNode(sll.head.next);
expect(sll.head.value).to.equal(3);
expect(sll.head.next.value).to.equal(1);
done();
});
});

describe('#SLL.reverse', function(){
it('reverses the SLL', function(done){
sll.reverse();
expect(sll.head.value).to.equal(1);
expect(sll.head.next.value).to.equal(2);
expect(sll.head.next.next.value).to.equal(3);
done();
});
});

});