2017-09-20 65 views
0
node* nodeArray[1000]; 
for (int j = 0; j < 1000; j++){ 
nodeArray[j] = new node; 
} 
int nodeCounter = 0; 
string temporary = ""; 
string cont; //file content 
int i = 0; 
while (getline(fileObject, cont)){ 
    for (int k = 0; k < cont.length(); k++) 
    cont[k] = tolower(cont[k]); 
    while (i < cont.length()){ 

這裏是問題出現的地方.Cout行告訴我,我的邏輯很好,因爲它應該將節點插入到我的鏈表列表中。但它實際上並沒有將它們添加到鏈表列表中。信息實際上並沒有存儲在節點陣列內

//cout << "nodeArray [" << nodeCounter << "] : " << temporary << "\n"; 
    insert(nodeArray[nodeCounter], temporary); 
    temporary = ""; 
    i++; 
} 
i = 0; 
nodeCounter++; 

} 

這裏還有可能與程序

void insertion(node* tail, string info){ 
     node* temp = new node; 
     temp->data = info; 
     temp->previous = tail; 
     temp->next = NULL; 
     tail = temp; 
    } 
+0

前兩個代碼片段是否來自主函數?請分享更清晰的代碼。 –

+0

是的,它來自主要功能。但是,插入功能是分開的。它恭維了一個標準的鏈表節點結構。 –

+1

您更改本地變量尾部。 –

回答

2

你是按值傳遞一個指針,而不是參考搞亂我的插入功能,因此地址傳入的變量指向是沒有改變。

變化 void insertion(node* tail, string info){void insertion(node*& tail, string info){

+0

解決了將這些問題添加到數組中的問題。現在我遇到的問題是,不是將節點添加到數組中的每個單獨鏈接列表中,而是將其覆蓋。 –

+0

@ZacharyCollins尾巴不是尾巴而是頭部。 –