Javascript 将单链接列表更改为双链接列表
我想从单链接列表构建双链接列表 输出为[1,10,5,16] 我认为问题出在append、insert和remove函数中 有人能帮我换一下单独的linkedList吗 我所知道的双链接列表有两个节点 一点到下一个值 上一个值的第二个点 这是我的JS:Javascript 将单链接列表更改为双链接列表,javascript,doubly-linked-list,Javascript,Doubly Linked List,我想从单链接列表构建双链接列表 输出为[1,10,5,16] 我认为问题出在append、insert和remove函数中 有人能帮我换一下单独的linkedList吗 我所知道的双链接列表有两个节点 一点到下一个值 上一个值的第二个点 这是我的JS: 类双链接列表{ 构造函数(值){ 这个头={ 价值:价值, 上一个:空, 下一个:空 }; this.tail=this.head; 这个长度=1; } 附加(值){ 常数newNode={ 价值:价值, 上一个:空, 下一个:空 } this.
类双链接列表{
构造函数(值){
这个头={
价值:价值,
上一个:空,
下一个:空
};
this.tail=this.head;
这个长度=1;
}
附加(值){
常数newNode={
价值:价值,
上一个:空,
下一个:空
}
this.tail.next=newNode;
newNode.previous=this.head
this.tail=newNode;
这个.length++;
归还这个;
}
预结束(值){
常数newNode={
价值:价值,
下一个:空
}
newNode.next=this.head;
this.head=newNode;
这个.length++;
归还这个;
}
打印列表(){
常量数组=[];
让currentNode=this.head;
while(currentNode!==null){
array.push(currentNode.value)
currentNode=currentNode.next
}
返回数组;
}
插入(索引、值){
//检查参数是否正确;
如果(索引>=此.length){
console.log('yes')
返回此.append(值);
}
常数newNode={
价值:价值,
下一个:空
上一个:空
}
const leader=this.traverseToIndex(索引-1);
const holdingPointer=leader.next;
leader.next=newNode;
newNode.previous=this.head
newNode.next=保持指针;
这个.length++;
返回这个.printList();
}
遍历索引(索引){
//检查参数
设计数器=0;
让currentNode=this.head;
while(计数器!==索引){
currentNode=currentNode.next;
计数器++;
}
返回当前节点;
}
删除(索引){
//检查参数
const leader=this.traverseToIndex(索引-1);
const unwantedNode=leader.next;
unwantedNode.previous=leader.head;
leader.next=unwantedNode.next;
这个长度--;
返回这个.printList();
}
}
设myDoublyLinkedList=newdoublylinkedlist(10);
myDoublyLinkedList.append(5);
myDoublyLinkedList.append(16);
myDoublyLinkedList.prepend(1);
myDoublyLinkedList.insert(2,6);
myDoublyLinkedList.remove(2)//应该返回[1,10,5,16]
我已经更新了追加
和前置
函数。现在应该可以正确显示了
class DoublyLinkedList {
constructor(value) {
this.head = {
value: value,
previous:null,
next: null
};
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = {
value: value,
previous:null,
next: null
}
this.tail.next = newNode;
newNode.previous=this.tail;
this.tail = newNode;
this.length++;
return this;
}
prepend(value) {
const newNode = {
value: value,
previous:null,
next: null
}
this.head.previous = newNode;
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
printList() {
const array = [];
let currentNode = this.head;
while(currentNode !== null){
array.push(currentNode.value)
currentNode = currentNode.next
}
return array;
}
insert(index, value){
//Check for proper parameters;
if(index >= this.length) {
console.log('yes')
return this.append(value);
}
const newNode = {
value: value,
next: null,
previous:null
}
const leader = this.traverseToIndex(index-1);
const holdingPointer = leader.next;
leader.next = newNode;
newNode.previous = leader;
newNode.next = holdingPointer;
this.length++;
return this.printList();
}
traverseToIndex(index) {
//Check parameters
let counter = 0;
let currentNode = this.head;
while(counter !== index){
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
remove(index) {
// Check Parameters
const leader = this.traverseToIndex(index-1);
const unwantedNode = leader.next;
leader.next = unwantedNode.next;
unwantedNode.next.previous=leader;
this.length--;
return this.printList();
}
}
let myDoublyLinkedList = new DoublyLinkedList(10);
myDoublyLinkedList.append(5);
myDoublyLinkedList.append(16);
myDoublyLinkedList.prepend(1);
myDoublyLinkedList.insert(2, 6);
myDoublyLinkedList.remove(2);//Should return [1,10,5,16]
我已经更新了
append
和prepend
函数。现在应该可以正确显示了
class DoublyLinkedList {
constructor(value) {
this.head = {
value: value,
previous:null,
next: null
};
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = {
value: value,
previous:null,
next: null
}
this.tail.next = newNode;
newNode.previous=this.tail;
this.tail = newNode;
this.length++;
return this;
}
prepend(value) {
const newNode = {
value: value,
previous:null,
next: null
}
this.head.previous = newNode;
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
printList() {
const array = [];
let currentNode = this.head;
while(currentNode !== null){
array.push(currentNode.value)
currentNode = currentNode.next
}
return array;
}
insert(index, value){
//Check for proper parameters;
if(index >= this.length) {
console.log('yes')
return this.append(value);
}
const newNode = {
value: value,
next: null,
previous:null
}
const leader = this.traverseToIndex(index-1);
const holdingPointer = leader.next;
leader.next = newNode;
newNode.previous = leader;
newNode.next = holdingPointer;
this.length++;
return this.printList();
}
traverseToIndex(index) {
//Check parameters
let counter = 0;
let currentNode = this.head;
while(counter !== index){
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
remove(index) {
// Check Parameters
const leader = this.traverseToIndex(index-1);
const unwantedNode = leader.next;
leader.next = unwantedNode.next;
unwantedNode.next.previous=leader;
this.length--;
return this.printList();
}
}
let myDoublyLinkedList = new DoublyLinkedList(10);
myDoublyLinkedList.append(5);
myDoublyLinkedList.append(16);
myDoublyLinkedList.prepend(1);
myDoublyLinkedList.insert(2, 6);
myDoublyLinkedList.remove(2);//Should return [1,10,5,16]