复制的队列更改 打印临时队列后队列的头部指针指向空 队列
Copied queue changes Queue's head pointer points to null after printing temp Queue
在dumpStack结束后,对printTable的调用会打印hashTable,每个索引中的队列都很好。当我之后尝试单独打印它时,head->next 指向 null(head 指向虚拟节点(。如何在不更改主队列指针的情况下打印临时队列?我不允许使用矢量。
void dumpStack(listNode *top, int currentDigit, int currentTable) {
while (isEmpty() == 0) {
listNode *temp = pop();
int digit = getDigit(temp, currentDigit);
int hashIndex = digit;
addTail(hashTable[currentTable][hashIndex], temp);
cout << "Added " << temp->data << " to hashTable[" << currentTable << "][" << hashIndex << "]" << endl;
cout << hashTable[currentTable][hashIndex]->head->next->data << endl;
}
cout << "DONE DUMPSTACK" << endl;
printTable(hashTable[currentTable]);
cout << hashTable[currentTable][9]->head->next->data << endl;
}
void printTable(linkedListQueue **ht) {
for (int i = 0; i < 10; i++) {
linkedListQueue *temp = ht[i];
if (isEmpty(temp) == 0) {
cout << "Table [" << currentTable << "][" << i << "]:";
while (temp->head->next != nullptr) {
cout << " " << temp->head->next->data;
if (temp->head->next->next != nullptr)
cout << ",";
temp->head->next = temp->head->next->next;
}
cout << endl;
}
}
}
我的代码的所有部分都是正确的,并且没有任何运行时错误。
我所做的是将 listLinkedQueue *temp 更改为 listNode *temp,并让 temp 等于 head->next,因为 head 在我的代码中代表一个虚拟节点。这样,当我遍历队列时,我永远不会更改队列的原始顺序。
void printTable(linkedListQueue **ht) {
for (int i = 0; i < 10; i++) {
listNode *temp = ht[i]->head->next;
if (temp != nullptr) {
cout << "Table [" << currentTable << "][" << i << "]:";
while (temp != nullptr) {
cout << " " << temp->data;
if (temp->next != nullptr)
cout << ",";
temp = temp->next;
}
cout << endl;
}
}
}
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 使用"Task"函数指针队列定义作业管理器
- 向量成员在管理类指针C++队列时丢失
- 更改队列指针成员的值需要在 C++ 中出现奇怪的错误
- 队列数组指针 (C++)
- 队列未正确插入指针
- 复制的队列更改 打印临时队列后队列的头部指针指向空 队列
- 为什么我的简单C++程序使用队列和 fstream 错误到无效指针
- 实现指针队列
- 用指针清理队列时,无尽的环路
- 从指针队列访问结构的成员
- 优先级队列实现为单一链接,无法更新插入的指针转换
- 如何通过队列指针访问存储在节点中的类中的数据
- 使用无锁指针队列在线程之间移动数据是否安全
- C++:在队列中丢失指针值
- 如何创建将保存指向函数的指针的队列
- 指针指向结构的指针(优先队列)
- 指针C++的优先级队列
- 矢量和指针队列数据交换:深层和浅层副本
- 将包含指向该类型队列的指针的结构推送到队列上时发生运行时崩溃