2016-06-10 65 views
1

我無法指向最後一個節點。帶5個節點的雙鏈表

輸出是假設是這樣的:

鏈接列表和5個節點

  1. 節點值:頭節點/下一個節點值:第二個節點/最後的節點值:null

  2. 節點值:第二節點/下一節點值:第三節點/最後節點值:頭節點

  3. 節點V ALUE:三節點/下一個節點值:四節點/最後的節點值:第二個節點

  4. 節點值:四節點/下一個節點值:尾節點/最後的節點值:三節點

  5. 節點值:尾節點/下一個節點值:未定義/最後的節點值:四節點

不過,我不斷收到這樣的:

鏈接列表和5個節點

  1. 節點值:頭節點/下一個節點值:第二個節點/最後的節點值:undefined

  2. 節點值:第二個節點/下一個節點值:三節點/最後的節點值:undefined

  3. 節點值:第三個節點/下一個節點值:四節點/最後的節點值:undefined

  4. 節點值:四節點/下一個節點值:尾節點/最後的節點值:undefined

  5. 節點值:尾節點/下一個節點值:未定義/最後的節點值:null

var DoubleLinkedList = function() { 
 

 
    this.head = 0; 
 
    this.tail = 0; 
 
    this.length = 5; 
 

 
    var LinkedListNode = function(content) { 
 
    this.next = 0; 
 
    this.last = []; 
 
    this.content = content; 
 
    }; 
 

 
    this.add = function(content) { 
 
    if (this.head == 0) { 
 
     this.head = new LinkedListNode(content); 
 
     return this.head; 
 
    } 
 
    if (this.tail == 0) { 
 
     this.tail = new LinkedListNode(content); 
 
     this.head.next = this.tail; 
 
     return this.tail; 
 
    }; 
 
    this.tail.next = new LinkedListNode(content); 
 
    this.tail = this.tail.next; 
 
    this.tail.next = 0; 
 
    return this.tail; 
 
    }; 
 
} 
 

 
DoubleLinkedList.prototype.length = function() { 
 
    var i = 0; 
 
    var node = this.head; 
 

 
    while (node != 0) { 
 
    i++; 
 
    node = node.next; 
 
    } 
 
    return i; 
 
}; 
 

 
DoubleLinkedList.prototype.toString = function() { 
 
    var i = 1; 
 
    var str = 'Linked List with ' + this.length + ' nodes <br/>'; 
 
    var node = this.head; 
 

 

 
    while (node != 0) { 
 
    str += i + ': Node Value: ' + node.content; 
 
    str += '/Next Node Value: ' + node.next.content; 
 
    str += "/Last Node Value: " + node.last; 
 

 
    if (node.next == 0) str += ' null'; 
 
    if (node.next != 0) str += node.last.content; 
 
    i++; 
 
    str += "<br>"; 
 
    node = node.next; 
 
    } 
 
    return str; 
 
}; 
 

 
var lln = new DoubleLinkedList(); 
 

 
lln.add(' Head Node'); 
 
lln.add(' Second Node'); 
 
lln.add(' Third Node'); 
 
lln.add(' Fourth Node') 
 
lln.add(' Tail Node'); 
 

 
document.getElementById('output').innerHTML = lln.toString();
<p id='output'></p>

+0

**我知道它可能是一些愚蠢和小事,但我一直在看這個小時,並找不到我做錯了什麼** – Natedizzy

+0

**這是jsfiddle鏈接** https://jsfiddle.net/7kd24pf7/11/ – Natedizzy

+0

請停止粗體顯示您輸入的所有內容。我已將您的代碼移到可運行的代碼片段中,以便人們更容易診斷問題。 –

回答

1

var DoubleLinkedList = function() { 
 

 
    this.head = 0; 
 
    this.tail = 0; 
 
    this.length = 5; 
 

 
    var LinkedListNode = function(content) { 
 
    this.next = 0; 
 
    this.last = 0; 
 
    this.content = content; 
 
    }; 
 

 
    this.add = function(content) { 
 
    if (this.head == 0) { 
 
     this.head = new LinkedListNode(content); 
 
     return this.head; 
 
    } 
 
    if (this.tail == 0) { 
 
     this.tail = new LinkedListNode(content); 
 
     this.head.next = this.tail; 
 
     this.tail.last = this.head; 
 
     return this.tail; 
 
    }; 
 
    this.tail.next = new LinkedListNode(content); 
 
    this.tail.next.last = this.tail; 
 
    this.tail = this.tail.next; 
 
    this.tail.next = 0; 
 
    return this.tail; 
 
    }; 
 
} 
 

 
DoubleLinkedList.prototype.length = function() { 
 
    var i = 0; 
 
    var node = this.head; 
 

 
    while (node != 0) { 
 
    i++; 
 
    node = node.next; 
 
    } 
 
    return i; 
 
}; 
 

 
DoubleLinkedList.prototype.toString = function() { 
 
    var i = 1; 
 
    var str = 'Linked List with ' + this.length + ' nodes <br/>'; 
 
    var node = this.head; 
 

 

 
    while (node != 0) { 
 
    str += i + ': Node Value: ' + node.content; 
 
    str += '/Next Node Value: ' + node.next.content; 
 
    str += "/Last Node Value: "; 
 

 
    if (node.last == 0) str += ' null'; 
 
    else str += node.last.content; 
 
    i++; 
 
    str += "<br>"; 
 
    node = node.next; 
 
    } 
 
    return str; 
 
}; 
 

 
var lln = new DoubleLinkedList(); 
 

 
lln.add(' Head Node'); 
 
lln.add(' Second Node'); 
 
lln.add(' Third Node'); 
 
lln.add(' Fourth Node') 
 
lln.add(' Tail Node'); 
 

 
document.getElementById('output').innerHTML = lln.toString();
<p id='output'></p>

您沒有設置val。last val在add方法中,並在toString方法中犯了一些錯誤。

+0

謝謝,我明白了! – Natedizzy

+0

我該如何去添加一個將節點添加到列表的用戶界面? – Natedizzy