Ich versuche, eine verknüpfte Listenfunktion zu schreiben, die den Knoten nach Suchwert entfernen kann. Wenn der Wert übereinstimmt, entfernt er den Knoten und verknüpft den vorherigen Knoten mit dem nächsten Knoten. Ich habe den Pseudocode geschrieben, aber ich habe Probleme bei der Implementierung. Die Funktion heißt removedSelected();Javascript verknüpfte Liste
var LinkedList = function(){
var list = {};
//this should always point to the first node in the list
list.head = null;
list.tail = null;
list.addToTail = function(value){
var newNode = Node(value);
if(!list.head){
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
};
list.removeSelected = function(target){
//loop through the linked list
//if head value is not equal to target
var node = this.head;
while(node.value !== target){
}
//keep going through the list
//if this.head === target
//point the previous next to the next node
};
list.indexCount = function(){
return this.indexCount;
}
list.removeHead = function(){
var deleted = this.head.value;
this.head = this.head.next;
if(this.head === null){
this.tail = null;
}
return deleted;
};
list.contains = function(target){
var node = this.head;
while(node !== null){
if(node.value === target){
return true;
}
node = node.next;
}
return false;
};
return list;
};
var Node = function(value){
var node = {};
node.value = value;
node.next = null;
return node;
};
var a = new LinkedList();
a.addToTail(1);
a.addToTail(5);
a.addToTail(55);
a.removeSelected(5);
Zunächst einmal * DANKE * für die Veröffentlichung eines vollständigen, brauchbaren Beispiels. Ich habe heute nicht viel Zeit, aber ich werde sehen, was ich tun kann. –
@ JeremyJStarcher Vielen Dank, dass Sie sich für einen Beitrag bedankt haben, der sich für ein vollständiges Beispiel ausgesprochen hat;) –