单项链表类
ES5写法
function LinkedList() {
let Node = function(element){
this.element = element;
this.next = null;
};
let length = 0;
let head = null;
this.append = function(element){
let node = new Node(element),
current;
if (head === null){ //first node on list 如果是空链表,就把当前元素作为链表头
head = node;
} else {
current = head;
//loop the list until find last item 循环查找尾元素
while(current.next){
current = current.next;
}
//get last item and assign next to added item to make the link
//将当前元素添加到链表尾部
current.next = node;
}
length++; //update size of list 更新链表长度
};
this.insert = function(position, element){
//check for out-of-bounds values
if (position >= 0 && position <= length){
let node = new Node(element),
current = head,
previous,
index = 0;
if (position === 0){ //add on first position
node.next = current;
head = node;
} else {
while (index++ < position){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
length++; //update size of list
return true;
} else {
return false;
}
};
this.removeAt = function(position){
//check for out-of-bounds values 检查越界值
if (position > -1 && position < length){
let current = head,
previous,
index = 0;
//removing first item 如果位置为0,移除第一个元素
if (position === 0){
head = current.next;
} else {
while (index++ < position){
previous = current;
current = current.next;
}
//link previous with current's next - skip it to remove
previous.next = current.next;
}
length--;
return current.element;
} else { //position越界返回null
return null;
}
};
this.remove = function(element){
let index = this.indexOf(element);
return this.removeAt(index);
};
this.indexOf = function(element){
let current = head,
index = 0;
while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -1;
};
this.isEmpty = function() {
return length === 0;
};
this.size = function() {
return length;
};
this.getHead = function(){
return head;
};
this.toString = function(){
let current = head,
string = '';
while (current) {
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string;
};
this.print = function(){
console.log(this.toString());
};
}
ES6写法
let LinkedList2 = (function () {
class Node {
constructor(element){
this.element = element;
this.next = null;
}
}
const length = new WeakMap();
const head = new WeakMap();
class LinkedList2 {
constructor () {
length.set(this, 0);
head.set(this, null);
}
append(element) {
let node = new Node(element),
current;
if (this.getHead() === null) { //first node on list
head.set(this, node);
} else {
current = this.getHead();
//loop the list until find last item
while (current.next) {
current = current.next;
}
//get last item and assign next to added item to make the link
current.next = node;
}
//update size of list
let l = this.size();
l++;
length.set(this, l);
}
insert(position, element) {
//check for out-of-bounds values
if (position >= 0 && position <= this.size()) {
let node = new Node(element),
current = this.getHead(),
previous,
index = 0;
if (position === 0) { //add on first position
node.next = current;
head.set(this, node);
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
//update size of list
let l = this.size();
l++;
length.set(this, l);
return true;
} else {
return false;
}
}
removeAt(position) {
//check for out-of-bounds values
if (position > -1 && position < this.size()) {
let current = this.getHead(),
previous,
index = 0;
//removing first item
if (position === 0) {
head.set(this, current.next);
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
//link previous with current's next - skip it to remove
previous.next = current.next;
}
let l = this.size();
l--;
length.set(this, l);
return current.element;
} else {
return null;
}
}
remove(element) {
let index = this.indexOf(element);
return this.removeAt(index);
}
indexOf(element) {
let current = this.getHead(),
index = 0;
while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -1;
}
isEmpty() {
return this.size() === 0;
}
size() {
return length.get(this);
}
getHead() {
return head.get(this);
}
toString() {
let current = this.getHead(),
string = '';
while (current) {
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string;
}
print() {
console.log(this.toString());
}
}
return LinkedList2;
})();
双向链表类
ES5写法
function DoublyLinkedList() {
let Node = function(element){
this.element = element;
this.next = null;
this.prev = null; //NEW
};
let length = 0;
let head = null;
let tail = null; //NEW
this.append = function(element){
let node = new Node(element),
current;
if (head === null){ //first node on list
head = node;
tail = node; //NEW
} else {
//attach to the tail node //NEW
tail.next = node;
node.prev = tail;
tail = node;
}
length++; //update size of list
};
this.insert = function(position, element){
//check for out-of-bounds values
if (position >= 0 && position <= length){
let node = new Node(element),
current = head,
previous,
index = 0;
if (position === 0){ //add on first position
if (!head){ //NEW
head = node;
tail = node;
} else {
node.next = current;
current.prev = node; //NEW {1}
head = node;
}
} else if (position === length) { //last item //NEW
current = tail; // {2}
current.next = node;
node.prev = current;
tail = node;
} else {
while (index++ < position){ //{3}
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
current.prev = node; //NEW
node.prev = previous; //NEW
}
length++; //update size of list
return true;
} else {
return false;
}
};
this.removeAt = function(position){
//check for out-of-bounds values
if (position > -1 && position < length){
let current = head,
previous,
index = 0;
//removing first item
if (position === 0){
head = current.next; // {1}
//if there is only one item, then we update tail as well //NEW
if (length === 1){ // {2}
tail = null;
} else {
head.prev = null; // {3}
}
} else if (position === length-1){ //last item //NEW
current = tail; // {4}
tail = current.prev;
tail.next = null;
} else {
while (index++ < position){ // {5}
previous = current;
current = current.next;
}
//link previous with current's next - skip it to remove
previous.next = current.next; // {6}
current.next.prev = previous; //NEW
}
length--;
return current.element;
} else {
return null;
}
};
this.remove = function(element){
let index = this.indexOf(element);
return this.removeAt(index);
};
this.indexOf = function(element){
let current = head,
index = -1;
//check first item
if (element == current.element){
return 0;
}
index++;
//check in the middle of the list
while(current.next){
if (element == current.element){
return index;
}
current = current.next;
index++;
}
//check last item
if (element == current.element){
return index;
}
return -1;
};
this.isEmpty = function() {
return length === 0;
};
this. size = function() {
return length;
};
this.toString = function(){
let current = head,
s = current ? current.element : '';
while(current && current.next){
current = current.next;
s += ', ' + current.element;
}
return s;
};
this.inverseToString = function() {
let current = tail,
s = current ? current.element : '';
while(current && current.prev){
current = current.prev;
s += ', ' + current.element;
}
return s;
};
this.print = function(){
console.log(this.toString());
};
this.printInverse = function(){
console.log(this.inverseToString());
};
this.getHead = function(){
return head;
};
this.getTail = function(){
return tail;
}
}
ES6写法
let DoublyLinkedList2 = (function () {
class Node {
constructor(element) {
this.element = element;
this.next = null;
this.prev = null; //NEW
}
}
const length = new WeakMap();
const head = new WeakMap();
const tail = new WeakMap(); //NEW
class DoublyLinkedList2 {
constructor () {
length.set(this, 0);
head.set(this, null);
tail.set(this, null);
}
append(element) {
let node = new Node(element),
current, _tail;
if (this.getHead() === null) { //first node on list
head.set(this, node);
tail.set(this, node); //NEW
} else {
//attach to the tail node //NEW
_tail = this.getTail();
_tail.next = node;
node.prev = _tail;
tail.set(this, node);
}
//update size of list
let l = this.size();
l++;
length.set(this, l);
}
insert(position, element) {
//check for out-of-bounds values
if (position >= 0 && position <= this.size()) {
let node = new Node(element),
current = this.getHead(),
previous,
index = 0;
if (position === 0) { //add on first position
if (!this.getHead()) { //NEW
head.set(this, node);
tail.set(this, node);
} else {
node.next = current;
current.prev = node; //NEW {1}
head.set(this, node);
}
} else if (position === this.size()) { //last item //NEW
current = tail; // {2}
current.next = node;
node.prev = current;
tail.set(this, node);
} else {
while (index++ < position) { //{3}
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
current.prev = node; //NEW
node.prev = previous; //NEW
}
//update size of list
let l = this.size();
l++;
length.set(this, l);
return true;
} else {
return false;
}
}
removeAt(position) {
//check for out-of-bounds values
if (position > -1 && position < this.size()) {
let _head = this.getHead(),
_tail = this.getTail(),
current = _head,
previous,
index = 0;
//removing first item
if (position === 0) {
_head = current.next; // {1}
//if there is only one item, then we update tail as well //NEW
if (this.size() === 1) { // {2}
_tail = null;
} else {
_head.prev = null; // {3}
}
} else if (position === this.size() - 1) { //last item //NEW
current = _tail; // {4}
_tail = current.prev;
_tail.next = null;
} else {
while (index++ < position) { // {5}
previous = current;
current = current.next;
}
//link previous with current's next - skip it to remove
previous.next = current.next; // {6}
current.next.prev = previous; //NEW
}
head.set(this,_head);
tail.set(this,_tail);
//update size of list
let l = this.size();
l--;
length.set(this, l);
return current.element;
} else {
return null;
}
}
remove(element) {
let index = this.indexOf(element);
return this.removeAt(index);
}
indexOf(element) {
let current = this.getHead(),
index = -1;
//check first item
if (element == current.element) {
return 0;
}
index++;
//check in the middle of the list
while (current.next) {
if (element == current.element) {
return index;
}
current = current.next;
index++;
}
//check last item
if (element == current.element) {
return index;
}
return -1;
}
isEmpty() {
return this.size() === 0;
}
size() {
return length.get(this);
}
toString() {
let current = this.getHead(),
s = current ? current.element : '';
while (current && current.next) {
current = current.next;
s += ', ' + current.element;
}
return s;
}
inverseToString() {
let current = this.getTail(),
s = current ? current.element : '';
while (current && current.prev) {
current = current.prev;
s += ', ' + current.element;
}
return s;
}
print() {
console.log(this.toString());
}
printInverse() {
console.log(this.inverseToString());
}
getHead() {
return head.get(this);
}
getTail() {
return tail.get(this);
}
}
return DoublyLinkedList2;
})();
- 我们可以对insert和remove这两个方法的实现做一些改进。在结果为否的 情况下,我们可以把元素插入到列表的尾部。性能也可以有所改进,比如,如果 position大于length/2,就最好从尾部开始迭代,而不是从头开始(这样就 能迭代更少列表中的元素)。
循环链表
- 循环链表和链表之间唯一的区别在于,最后一个元素指向下一个元素的指针(tail.next)不是引用null, 而是指向第一个元素(head)
- 双向循环链表有指向head元素的tail.next,和指向tail元素的head.prev。