Skip to content

Commit

Permalink
* First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Micky Brunetti committed Feb 2, 2014
0 parents commit 3b26353
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Wizcorp

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "CircularDoublyList",
"repo": "Wizcorp/list-CircularDoubly",
"version": "0.1.0",
"scripts": [
"index.js"
],
"license": "MIT"
}
159 changes: 159 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* CIRCULAR DOUBLY LIST Class
*
* @author Brice Chevalier
*
* @desc Doubly list data structure
*
* Method Time Complexity
* ___________________________________
*
* add O(1)
* remove O(n)
* removeByRef O(1)
* getFirst O(1)
* getLast O(1)
* getCount O(1)
* apply O(n)
* clear O(n)
*
* Memory Complexity in O(n)
*/

function Node(obj, previous, next) {
this.object = obj;
this.previous = previous;
this.next = next;
}

function CircularDoublyList() {
this.count = 0;
this.last = null;
this.first = null;
}

CircularDoublyList.prototype.add = function (obj) {
this.count += 1;
var newNode = new Node(obj, this.last, this.first);
if (this.first === null) {
this.first = newNode;
this.last = newNode;
newNode.previous = newNode;
newNode.next = newNode;
} else {
// insertion before the this.last one
this.last.next = newNode;
this.last = newNode;
this.first.previous = newNode;
}
return newNode;
};

CircularDoublyList.prototype.remove = function (obj) {
if (this.first === this.last) {
if (this.first.object === obj) {
this.first = null;
this.last = null;
this.count -= 1;
}
return;
}

var current = this.first;
while (current !== this.last) {
if (current.object === obj) {

// Removing any reference to the node
current.next.previous = current.previous;
if (current === this.first) {
this.first = current.next;
} else {
current.previous.next = current.next;
}

// Removing any reference from the node to any other element of the list
current.previous = null;
current.next = null;

this.count -= 1;
return;
}
current = current.next;
}

if (current.object === obj) {

// Removing any reference to the node
this.last = current.previous;
if (current === this.first) {
this.first = current.next;
} else {
current.previous.next = current.next;
}

// Removing any reference from the node to any other element of the list
current.previous = null;
current.next = null;

this.count -= 1;
return;
}
};

CircularDoublyList.prototype.removeByRef = function (node) {

// Removing any reference to the node
if (node.next === null) {
this.last = node.previous;
} else {
node.next.previous = node.previous;
}
if (node.previous === null) {
this.first = node.next;
} else {
node.previous.next = node.next;
}

// Removing any reference from the node to any other element of the list
node.previous = null;
node.next = null;

this.count -= 1;
};

CircularDoublyList.prototype.getFirst = function () {
return this.first;
};

CircularDoublyList.prototype.getLast = function () {
return this.last;
};

CircularDoublyList.prototype.clear = function () {
this.first = null;
this.last = null;
};

CircularDoublyList.prototype.getCount = function () {
return this.count;
};

CircularDoublyList.prototype.forEach = function (processingFunc, params) {
for (var current = this.first; current !== this.last; current = current.next) {
processingFunc(current.object, params);
}
if (current !== null) {
processingFunc(current.object, params);
}
};

CircularDoublyList.prototype.forEachReverse = function (processingFunc, params) {
for (var current = this.last; current !== this.first; current = current.next) {
processingFunc(current.object, params);
}
if (current !== null) {
processingFunc(current.object, params);
}
};

module.exports = CircularDoublyList;

0 comments on commit 3b26353

Please sign in to comment.