2013-04-03 51 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; 
     } 

我也不能看到你把項目從隊列前面的代碼的任何部分。這通常是你會做的。