Skip to content

Shelly #19

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-shelly/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;
};
23 changes: 23 additions & 0 deletions lab-shelly/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "lab-shelly",
"version": "1.0.0",
"description": "data structure",
"main": "list-node.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"keywords": [
"data",
"structures",
"lab"
],
"author": "shelly",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.3.0"
}
}
84 changes: 84 additions & 0 deletions lab-shelly/sll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

const Node = require('./list-node');
// [val, next]

// (HEAD)[val, next] The list is empty, so we just 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 last node

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

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

//O(n)

SLL.prototype.remove = function(value) {

if(this.head == null) return;
//if head is what needs to be deleted, then reassign the head position. make head be equal to the node with value head.next
if(this.head == value) {
this.head = this.head.next;
return;
}

let current = this.head;

while(current.next != null) {
if (current.next.value === value) {
current.next = current.next.next;
return;
}
current = current.next; //otherwise, continue to iterate until if is met
}
//iterate thru the link list and delete one before the element we want to delete. set current nodes next pointer to equal the to-be-deleted values next pointer - so...you're skipping over the deleted one and popping it out.
};

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

while(current != null) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
this.head = previous;
};
10 changes: 10 additions & 0 deletions lab-shelly/test/sll-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

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

// let sll = new SLL();

// console.log(sll); //head: null;
// sll.prepend(100);
// console.log(sll); // head { value: 100 next: null }
// console.log(sll.prepend(200));