2013-04-26 21 views
1

我試圖編寫一個函數來顛倒循環鏈表(1哨兵,雙鏈)的順序。以下是我的代碼。原來的列表是15,14,11,12。我期望新列表是12,11,14和15.但是我一直得到15,14,11和12.有人可以看我的代碼,並給予我有些提示?謝謝!爲什麼我的代碼在倒轉循環鏈表時無效?

void reverseCirListDeque(struct cirListDeque *q) 
{ 

    assert(q != 0); 
    assert(!isEmptyCirListDeque(q)); 


    struct DLink *start = q->Sentinel->next; 
    struct DLink *next; 
    struct DLink *prev = NULL; 

    while (start != NULL) 
    { 
     //Swap the next and previous link 
     next = start->next; 
     start->next = prev; 
     prev = start; 
     start = next; 
    }  

} 
+0

我認爲你需要非常緩慢地瀏覽代碼,並確保每一行都有意義。也許可以打印出代碼並寫出每行的簡短說明。你說對了,整個程序對我們來說很重要,但是單一的功能太少了。這可能有助於編寫一些簡單的程序,也許單鏈表,直到你對指針和結構有更深的理解。 – 2013-04-26 07:24:40

+0

在這段代碼中,q-> Sentinal-> next'在剛剛顛倒的列表的頭部建立了它的立足點? – WhozCraig 2013-04-26 07:51:36

回答

1
next = start->next; 
start->next = prev; 
start->prev = next;//This line is missing 
prev = start; 
start = next; 

這將有錯訪問(或更新)的變量,這樣的邏輯似乎沒有問題。

相關問題