信息实际上并未存储在节点阵列中

Information not actually being stored inside array of nodes

本文关键字:节点 阵列 存储 实际上 信息      更新时间:2023-10-16
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行告诉我,我的逻辑很好,因为它应该在我的linkedlists数组中插入节点。但这实际上并不是将它们添加到linkedlist的数组中。

    //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;
  }

您是按值而不是参考传递指针,因此传递变量指向的地址没有更改。

更改 void insertion(node* tail, string info){进入 void insertion(node*& tail, string info){