2014-10-03 216 views
0

因此,我在C++中遇到了一些關於鏈表的問題。當我嘗試創建新列表時,最近的節點是列表中唯一的一個。我認爲這是一個問題,指向它插入後節點的指針,但我不確定,因爲我是新手C++
和鏈接列表。任何援助非常歡迎!鏈接列表問題C++

// Nodes struct is declared, "int value" is data stored; 
    void Class::addNode(int val) 
    { 
     nodeptr temp; 
     temp = new node; 
     temp->value = val; 
     temp->next = nullptr; 
     nodeptr crt, prv; 
     for (prv = nullptr, crt = headPtr; /* pointer to first node */ 
     crt != nullptr && temp->value < crt->value; prv->next = crt, crt = crt->next) 
     { 
      // nothing because it's just to get crt and prv in position 
     } 
     if (crt = headPtr) 
     { 
      headPtr = temp; 
     } 
     else 
     { 
      prv->next = temp; 
     } 
     temp->next = crt; 
    } 
+2

'如果(CRT = headPtr)'應該是'如果(CRT == headPtr)'' – gsamaras 2014-10-03 00:59:13

+1

點ptr'到'nullptr'當你提領。 – 0x499602D2 2014-10-03 00:59:22

回答

0

塊:

if (crt = headPtr) 
    { 
     headPtr = temp; 
    } 

要設置CRT到headPtr,而不是檢查,如果它等於headPtr所以它總是落入條件和設置headPtr到REMP。應該是這樣的:

if (crt == headPtr) 
    { 
     headPtr = temp; 
    }