链表指针
Linked list pointers
我有一个指针的问题。我正在尝试使用链表队列进行广度优先状态空间搜索,但是我在创建队列(或者更确切地说将其链接在一起)时遇到了麻烦。下面是代码片段:
typedef struct queueList {
STATE *state;
queueList *next;
queueList(STATE *state): state(state), next(NULL) {}
~queueList() {delete next;}
} QUEUE_ELEMENT;
void moveAround(STATE *start) {
QUEUE_ELEMENT *queueBegin = new QUEUE_ELEMENT(start);
QUEUE_ELEMENT *queueEnd;
queueBegin->next = queueEnd;
while (queueBegin != NULL) {
STATE *current = queueBegin->state;
if (compareStates(current,finish) == 1) {
answer = current;
return;
}
for (int i = 0; i < 12; i++) {
STATE *newState = expandState(current, i);
if (newState != NULL) {
queueEnd = new QUEUE_ELEMENT(newState);
queueEnd = queueEnd->next;
}
}
queueBegin = queueBegin->next;
}
}
出什么事了?queueBegin->next没有被分配给任何东西,即使它应该(已经找到一个可能的状态)。
代码有问题,但我可以看到问题
QUEUE_ELEMENT *queueEnd;
queueBegin->next = queueEnd;
queueEnd
是未初始化的变量。
看更多我猜你想要queueEnd指向队列的末尾,当expandState返回非NULL时,你想要将新状态附加到队列。不幸的是,您编写的代码并没有这样做。我猜有点,但这看起来更接近
QUEUE_ELEMENT *queueBegin = new QUEUE_ELEMENT(start);
QUEUE_ELEMENT *queueEnd = queueBegin;
...
STATE *newState = expandState(current, i);
if (newState != NULL) {
QUEUE_ELEMENT *newQueueEnd = new QUEUE_ELEMENT(newState);
queueEnd->next = newQueueEnd;
queueEnd = newQueueEnd;
}
我也看不出代码的任何部分,你把项目从队列的前面。这是你通常会做的
相关文章:
- C++,指针数组,指向双链表中的条目
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 链表指针问题
- 将链表转换为指针数组时出错
- 如何实现容纳整数和无效指针的双向链表?
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 为什么我在空指针错误(链表)中获取成员访问权限
- 链表删除功能的单指针 // 是可能的
- 如何深度复制链表对象指针
- 链表 - 使用后面的指针在末尾插入
- 链表和指针在C++中的点
- 未分配被释放的指针(将堆栈实现为链表时)
- 使用双指针和递归反转链表
- 使用函数引用指向节点的指针删除链表中的节点?
- 使用 XOR 操作仅使用 2 个指针反转链表
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 指针永远不会在链表深层复制构造函数中达到 null
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 如何从文件读取到动态分配的指向结构的指针链表中?
- C++指针链表