Skip to content

lab-glen ready for turn in #1

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 7 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
3 changes: 3 additions & 0 deletions lab-glen/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/node_modules/*
**/vendor/*
**/*.min.js
25 changes: 25 additions & 0 deletions lab-glen/.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-glen/.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
10 changes: 10 additions & 0 deletions lab-glen/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="lib/sll.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions lab-glen/lib/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function Node (value) {
this.val = value;
this.next = null;
};
151 changes: 151 additions & 0 deletions lab-glen/lib/sll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
'use strict';

const Node = require('./node.js');

//O(n)
const LinkedList = module.exports = function () {
this.head = null;
this.tail = null;
this.listLength = 0;
};

//O(n)
LinkedList.prototype.size = function () {
let current = this.head;
let count = 0;

while(current !== null) {
count++;
current = current.next;
}
return count;
};

//O(1)
LinkedList.prototype.isEmpty = function () {
return this.head === null;
};

//O(1)
LinkedList.prototype.prepend = function (value) {
let node = new Node(value);
if(!this.head) {
this.head = node;
return this;
}

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

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

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

var current = this.head;

while(current.next !== null) {
current = current.next;
}
current.next = node;
this.listLength++;
};

//O(n)
LinkedList.prototype.findNthNode = function(value) {
let current = this.head;
let count = 0;

if(value > this.length) {
return 'Does not exist';
}

while(count < value) {
current = current.next;
count++;
}

return current;
};

//O(n)
LinkedList.prototype.findMiddleNode = function() {
let pointerTwo = this.head;
let pointerOne = this.head;

while(pointerTwo.next !== null && pointerTwo.next.next !== null) {
pointerTwo = pointerTwo.next.next;
pointerOne = pointerOne.next;
}
return pointerOne;
};

//O(1)
LinkedList.prototype.remove = function(val) {
if(this.head.val === val) {
this.head = this.head.next;
return;
}

let previous = null;
let current = this.head;

while(current.val !== val) {
previous = current;
current = current.next;

}
previous.next = current.next;
this.tail = current.next;
};

// O(n) where n is the number of nodes in the linked list
LinkedList.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;
};

//O(1)
LinkedList.prototype.contains = function (val) {
let current = this.head;
while(current !== null); {
if(current.val === val) {
return true;
}
current = current.next;
}
return false;
};

//O(1)
LinkedList.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);
};
18 changes: 18 additions & 0 deletions lab-glen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lab-glen",
"version": "1.0.0",
"description": "",
"main": "sll.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.3.0"
}
}
Loading