2013-04-03 90 views
0

我有一个指针问题。我试图做一个使用链表队列的宽度优先的状态空间搜索,但是我在创建队列(或者将它连接在一起)时遇到了困难。以下是片段:链接列表指针

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没有被分配给任何东西,即使它应该(可能的状态已被找到)。以下

+0

当你使用调试器时,你遇到什么问题? –

回答

0

故障代码,但我可以看到麻烦

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

我也不能看到你把项目从队列前面的代码的任何部分。这通常是你会做的。