2017-03-02 28 views
-5

我知道在while循環中發生了段錯誤:(while(temp != NULL){temp = temp->next;}),但我不知道爲什麼。class C++中的鏈表實現,顯示分段錯誤

#include<iostream> 

using namespace std; 

class zDepthList { 

     typedef struct node { 
       int data; 
       node* next; 
       node* prev; 
     } Node; 

public: 

     zDepthList() { 
       head = NULL; 
     } 

     zDepthList(int array[], int length) { 

       Node *temp, *ptr; 
       int i = 0; 

       while(i != length - 1) { 
         temp = head; 
         ptr = new Node; 
         ptr->data = array[i]; 
         i++; 
         ptr->next = NULL; 

         if(head == NULL) { 
           head = ptr; 
           ptr->prev = NULL; 
         } 

         else { 
           while(temp != NULL) { 
             temp = temp->next; 
           } 
         } 
         temp->next = ptr; 
         ptr->prev = temp; 
       } 
     } 

     void out(const char order) { 

       cout << head->data << endl; 

     return; 
     } 

private: 
     Node *head; 
}; 
+0

我們需要了解你在主程序中如何使用這個類。 – vincent

+0

我們不應該爲你做你的(家)工作。 –

+0

我的主要傳遞數組的30個元素和數組的長度(zDepthList z(l,30);)。它調出函數(z.out('f'))。 – aashman

回答

1

對於初學者,您必須將head初始化爲NULL

而且這個while循環

    else { 
          while(temp != NULL) { 
            temp = temp->next; 
          } 
        } 
        temp->next = ptr; 
        ptr->prev = temp; 

指針temp後等於NULL,因爲它是中斷循環的條件。因此,這種說法

    temp->next = ptr; 

導致未定義的行爲。

如果您有一個雙鏈表,那麼也很自然的介紹數據成員tail它可以很容易地添加新節點。

所以,你應該包括

class zDepthList { 
//... 
private: 
     Node *head, *tail; 
}; 

在這種情況下,構造可以看看下面的方式

zDepthList() : head(nullptr), tail(nullptr) 
    { 
    } 

    zDepthList(const int a[], size_t n) : head(nullptr), tail(nullptr) 
    { 
     for (size_t i = 0; i < n; i++) 
     { 
      Node *tmp = new Node { a[i], nullptr, tail }; 
      tail == nullptr ? head = tmp : tail->next = tmp; 
      tail = tmp; 
     } 
    } 

這裏是一個示範項目

#include <iostream> 

class zDepthList { 

    typedef struct node { 
     int data; 
     node* next; 
     node* prev; 
    } Node; 

public: 

    zDepthList() : head(nullptr), tail(nullptr) 
    { 
    } 

    zDepthList(const int a[], size_t n) : head(nullptr), tail(nullptr) 
    { 
     for (size_t i = 0; i < n; i++) 
     { 
      Node *tmp = new Node{ a[i], nullptr, tail }; 
      tail == nullptr ? head = tmp : tail->next = tmp; 
      tail = tmp; 
     } 
    } 


    std::ostream & out(std::ostream &os = std::cout) const 
    { 
     for (Node *current = head; current; current = current->next) 
     { 
      os << current->data << ' '; 
     } 

     return os; 
    } 

private: 
    Node *head, *tail; 
}; 

int main() 
{ 
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

    zDepthList l(a, sizeof(a)/sizeof(*a)); 

    l.out() << std::endl; 
} 

程序輸出是

0 1 2 3 4 5 6 7 8 9 
+0

非常感謝。我嚇壞了烤tho大聲笑 – aashman

+0

@aashman沒有。不用謝。 –

1

您從未設置過head,但您可以訪問它。這意味着它是未初始化的,這是一個UB。

只有在沒有任何參數的情況下調用它時,您纔有2個參數並初始化爲head

+0

頭部設置在構造函數中 – user4581301

+0

我在第二個構造函數的開頭將頭部設置爲NULL,但seg故障仍然存在。 – aashman

+0

如果'head'是'NULL',你仍然執行'temp = head',然後'temp-> next = ptr''NULL'' temp'。解引用空指針是未定義的行爲。 –