Make a Linked list-JS的單向鏈結
/**
* .
* append, removeAt, indexOf, remove, toString, size, getHead, insert, isEmpty
*/
const DoubleLinked = function () {
const Node = function (element) {
this.element = element;
this.next = null;
this.prev = null;
}
let length = 0;
let head, tail;
this.append = (element) => {
let node = new Node(element);
if(!head){
head = node;
tail = node;
return head.element;
}else{
tail.next = node;
node.prev = tail;
tail = node;
return tail.element;
}
}
this.insert = (position, element) => {
if (position > -1 && position <= length) {
const node = new Node(element);
let current = head;
let previous;
if (position === 0) {
if (!head) {
head = node;
tail = node;
} else {
node.next = current;
current.prev = node;
head = node;
}
} else if (position === length) {
current = tail;
current.next = node;
node.prev = current;
tail = node;
} else {
let index = 0;
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = node;
node.prev = previous;
node.next = current;
current.prev = node;
}
length++;
return head;
} else {
return false;
}
}
this.removeAt = (position) => {
if (position > -1 && position <= length) {
let current = head;
let previous;
if (position === 0) {
if (head) {
head = current.next;
// head.prev = null;
} else {
return false;
}
} else if (position === length) {
tail = tail.prev;
tail.next = null;
} else {
let index = 0;
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;
}
length--;
return current.element;
} else {
return false
}
};
this.indexOf = (element) => {
let current = head;
let index = 0;
while (current) {
if (current.element === element) {
return index;
} else {
return false;
}
index++;
current = current.next;
}
};
this.remove = (element) => {
let position = this.indexOf(element);
if (position >= 0) {
return this.removeAt(position);
} else {
return null;
}
}
this.toString = () => {
if(head){
let current = head;
let string = '';
while(current){
string += current;
current = current.next;
}
}else{
return null;
}
};
this.isEmpty = () => {
return length === 0;
};
this.getHead = () => {
return head;
};
this.getTail = () => {
return tail;
};
this.getSize = () => {
return hength;
};
}
const assert = require('assert');
describe('DoubleLinked', () => {
let a = new DoubleLinked();
describe('#insert()', () => {
it('Given link of Empty when enter position and element then check value.', () => {
let test = a.insert(0, "a").element;
assert.equal("a", test, "The result: " + test);
});
});
describe('#append()', () => {
it('Should allow append to last one.', () => {
let test = a.append("b");
assert.equal("b", test, "The result: " + test);
});
});
describe('#indexOf()', () => {
it('Should return index of element.', () => {
let test = a.indexOf("a");
assert.equal(0, test, "The result: " + test);
})
});
describe('#removeAt()', () => {
it('Should through position to remove.', () => {
let test = a.removeAt(0);
assert.equal("a", test, "The result: " + test);
})
});
describe('#remove()', () => {
it('Should through element to remove.', () => {
let test = a.remove('b');
assert.equal("b", test, "The result: " + test);
})
});
})Published 26 Jul 2017