[英]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;
}
另外,我看不到代码的任何部分将项目从队列的最前面移开。 那通常就是你要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.