\$\begingroup\$
\$\endgroup\$
What do you think guys?
module.exports = CDoublyLinkedList = function CDoublyLinkedList() {
this._size = 0;
this.first = undefined;
this.last = undefined;
}
CDoublyLinkedList.prototype.iterator = function(data) {
return new this.CIterator(this);
}
CDoublyLinkedList.prototype.size = function(data) {
return this._size;
}
CDoublyLinkedList.prototype.push = function(data) {
var item = new this.CItem();
item.data = data;
if ( this._size == 0 ) {
this.first = this.last = item;
}
else {
item.prev = this.last;
item.prev.next = item;
this.last = item;
}
this._size++;
}
CDoublyLinkedList.prototype.pop = function() {
if ( this._size == 0 ) {
return undefined;
}
var poped = this.last;
this.last = this.last.prev;
this._size--;
return poped;
}
CDoublyLinkedList.prototype.unshift = function(data) {
var item = new this.CItem();
item.data = data;
if ( this._size == 0 ) {
this.first = this.last = item;
}
else {
item.next = this.first;
this.first = item;
}
this._size++;
}
CDoublyLinkedList.prototype.shift = function() {
if ( this._size == 0 ) {
return undefined;
}
var shifted = this.first;
this.first = this.first.next;
this._size--;
return shifted;
}
CDoublyLinkedList.prototype._getSet = function(index, value) {
if ( index < 0 ) return undefined;
var i = 0;
var next = this.first;
while(next) {
if ( i === index ) return (value ? (next.data = value) : next.data);
next = next.next;
i++;
}
return undefined;
}
CDoublyLinkedList.prototype.get = function(index) {
return this._getSet(index);
}
CDoublyLinkedList.prototype.set = function(index, value) {
return this._getSet(index, value);
}
CDoublyLinkedList.prototype.remove = function(data) {
var curr = this.first;
while(curr) {
if ( curr.data === data ) {
curr.removed = true;
if ( this.first === curr ) this.first = curr.next;
if ( this.last === curr ) this.last = curr.prev;
if ( curr.prev ) curr.prev.next = curr.next;
if ( curr.next ) curr.next.prev = curr.prev;
this._size--;
return true;
}
curr = curr.next;
}
return false;
}
CDoublyLinkedList.prototype.toString = function(fn) {
var curr = this.first;
var str = "CDoublyLinkedList(";
while(curr) {
if ( fn ) str += fn(curr.data);
else str += JSON.stringify(curr.data);
if ( curr != this.last ) str += ",";
curr = curr.next;
}
return str + ")";
}
// ######################################################################### CItem
CDoublyLinkedList.prototype.CItem = function CItem() {
this.prev = undefined;
this.next = undefined;
this.data = undefined;
this.removed = false;
}
CDoublyLinkedList.prototype.CItem.prototype.prev = function() { return this.prev; }
CDoublyLinkedList.prototype.CItem.prototype.next = function() { return this.next; }
CDoublyLinkedList.prototype.CItem.prototype.data = function() { return this.data; }
CDoublyLinkedList.prototype.CItem.prototype.clear = function() { this.prev = this.next = this.data = undefined; }
// ######################################################################### CIterator
CDoublyLinkedList.prototype.CIterator = function CIterator(list) {
this.list = list;
this.current = undefined;
}
CDoublyLinkedList.prototype.CIterator.prototype.reset = function() {
this.current = undefined;
}
CDoublyLinkedList.prototype.CIterator.prototype.copyState = function(iterator) {
if ( iterator.list !== this.list ) throw new Error("CIterator list mismatch");
this.current = iterator.current;
}
CDoublyLinkedList.prototype.CIterator.prototype.next = function() {
if ( !this.current ) {
if ( !this.list.first ) return;
else {
this.current = this.list.first;
return this.current.data;
}
}
else {
this.current = this.current.next;
if ( this.current ) {
if ( this.current.removed ) return this.next();
return this.current.data;
}
}
return;
}
CDoublyLinkedList.prototype.CIterator.prototype.prev = function() {
if ( !this.current ) {
if ( !this.list.last ) return;
else {
this.current = this.list.last;
return this.current.data;
}
}
else {
this.current = this.current.prev;
if ( this.current ) {
if ( this.current.removed ) return this.prev();
return this.current.data;
}
}
return;
}
1 Answer 1
\$\begingroup\$
\$\endgroup\$
LGTM, the only nit I would have is using ===
for comparing this._size
to 0 for extreme type safety.
I didn't test for functionality, but I assume that by posting here, you've tested it and everything works.
answered Jul 7, 2017 at 2:15
Explore related questions
See similar questions with these tags.
default