简体   繁体   English

链接列表指针

[英]Linked list pointers

I have a problem with pointers. 我的指针有问题。 I'm trying to do a breadth-first state space search using a linked list queue, but I have trouble creating the queue (or rather linking it together). 我正在尝试使用链接列表队列进行广度优先状态空间搜索,但是在创建队列(或将其链接在一起)时遇到了麻烦。 Here's the snippet: 这是代码段:

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

What went wrong? 什么地方出了错? queueBegin->next is not being assigned to anything, even though it should (a possible state has been found). 即使应该将queueBegin-> next分配给任何内容(已找到可能的状态)。

Trouble following the code but I can see trouble 无法遵循代码,但我可以看到麻烦

QUEUE_ELEMENT *queueEnd;
queueBegin->next = queueEnd;

queueEnd is an uninitialised variable. queueEnd是一个未初始化的变量。

Looking more I'm guessing you want queueEnd to point to the end of the queue and when expandState returns non NULL you want to append the new state to the queue. 看得更多,我猜您想让queueEnd指向队列的末尾,并且当expandState返回非NULL时,您想将新状态附加到队列中。 Unfortunately the code you've written doesn't do anything like that. 不幸的是,您编写的代码无法执行任何操作。 I'm guessing somewhat but this looks a bit closer 我有点猜测,但这看起来有点近

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

Also I can't see any part of the code where you take items off the front of the queue. 另外,我看不到代码的任何部分将项目从队列的最前面移开。 That's normally what you would do. 那通常就是你要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM