2017-03-02 45 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'。解引用空指针是未定义的行为。 –