function DoublyLinkedList() {
function Node(element) {
this.element = element;
this.prev = null;
this.next = null;
}
this.length = 0;
this.head = null;
this.tail = null;
DoublyLinkedList.prototype.append = (element) => {
const node = new Node(element);
if (this.length === 0) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
this.length += 1;
};
DoublyLinkedList.prototype.insert = (position, element) => {
if (position < 0 || position > this.length) return false;
const node = new Node(element);
if (this.length === 0) {
this.head = node;
this.tail = node;
} else {
if (position === 0) {
this.head.prev = node;
node.next = this.head;
this.head = node;
} else if (position === this.length) {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
} else {
let current = this.head;
let previous = null;
let index = 0;
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
node.prev = previous;
previous.next = node;
current.prev = node;
}
}
this.length += 1;
return true;
};
DoublyLinkedList.prototype.get = (position) => {
if (position < 0 || position >= this.length) return null;
let current = null;
let index = null;
if (this.length / 2 > position) {
index = 0;
current = this.head;
while (index++ < position) {
current = current.next;
}
} else {
index = this.length - 1;
current = this.tail;
while (index-- > position) {
current = current.prev;
}
}
return current.element;
};
DoublyLinkedList.prototype.indexOf = (element) => {
let index = 0;
let current = this.head;
while (current) {
if (current.element === element) {
return index;
}
current = current.next;
index++;
}
return -1;
};
DoublyLinkedList.prototype.update = (position, element) => {
if (position < 0 || position >= this.length) return false;
let index = 0;
let current = null;
if (this.length / 2 > position) {
current = this.head;
while (index++ < position) {
current = current.next;
}
} else {
current = this.tail;
index = this.length - 1;
while (index-- > position) {
current = current.prev;
}
}
current.element = element;
};
DoublyLinkedList.prototype.removeAt = (position) => {
if (position < 0 || position >= this.length) return false;
let current = this.head;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
if (position === 0) {
this.head.next.prev = null;
this.head = this.head.next;
} else if (position === this.length - 1) {
current = this.tail;
this.tail.prev.next = null;
this.tail = this.tail.prev;
} else {
let index = 0;
while (index++ < position) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
this.length -= 1;
return current.element;
};
DoublyLinkedList.prototype.remove = (element) => {
const position = this.indexOf(element);
return this.removeAt(position);
};
DoublyLinkedList.prototype.toString = () => {
return this.backwordString();
};
DoublyLinkedList.prototype.backwordString = () => {
let current = this.head;
let resultString = "";
while (current) {
resultString += current.element + " ";
current = current.next;
}
return resultString;
};
DoublyLinkedList.prototype.forwardString = () => {
let current = this.tail;
let resultString = "";
while (current) {
resultString += current.element + " ";
current = current.prev;
}
return resultString;
};
DoublyLinkedList.prototype.size = () => {
return this.length;
};
DoublyLinkedList.prototype.isEmpty = () => {
return this.length === 0;
};
DoublyLinkedList.prototype.getHead = () => {
return this.head.element;
};
DoublyLinkedList.prototype.getTail = () => {
return this.tail.element;
};
}
const doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.append("【1】我是第一条数据");
doublyLinkedList.append("【2】我是第二条数据");
doublyLinkedList.append("【3】我是第三条数据");
doublyLinkedList.insert(3, "【99】我是插入的数据");
console.log("doublyLinkedList.toString()", doublyLinkedList.toString());
doublyLinkedList.remove("【2】我是第二条数据");
console.log("doublyLinkedList.toString()", doublyLinkedList.toString());