//双向循环链表
class DbCirList extends DbList {
constructor() {
super();
this.head.next = this.head;
this.head.prev = this.head;
}
/**
*
* @param item //插入的位置
* @param element 插入的值
*/
insert(item: any, element: any): void {
if(this.find(item)) {
return ;
}
if(this.find(element)) {
return;
}
let currNode = this.find(item);
let newNode = new NodeItem(element);
newNode.next = currNode.next;
newNode.prev = currNode;
currNode.next&&(currNode.next.prev = newNode);
currNode.next = newNode;
this.size++;
}
/**
*
* @param item 删除的值
*/
remove(item: any): void {
if (item === 'head') {
this.head.next = this.head;
this.head.prev = this.head;
this.size = 0;
return;
}
let currNode = this.find(item);
if(currNode.next&&currNode.prev) { // 如果存在值为item的节点
currNode.next.prev = currNode.prev;
currNode.prev.next = currNode.next;
this.size--;
}
}
/**
*
* @param element 添加的值
*/
append(element: any): void {
let newNode = new NodeItem(element);
let lastNode = this.findLast();
newNode.next = lastNode.next;
lastNode.next&&(lastNode.next.prev = newNode);
lastNode.next = newNode;
newNode.prev = lastNode;
this.size++;
}
}
let test = new DbCirList()
let arr = [1, 2, 3, 4, 5, 6, 7];
for(let i=0; i<arr.length; i++) {
test.append(arr[i]);
}
test.remove(1);
test.display();