2017-03-02 5 views
-5

세그먼트 반복 오류가이 while 루프에서 발생한다는 것을 알고 있습니다 : (while(temp != NULL){temp = temp->next;}), 그 이유는 알 수 없습니다.Segmentation 오류를 표시하는 클래스 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 (1, 30);). 그것은 함수를 호출합니다 (z.out ('f')). – aashman

답변

1

처음에는 headNULL으로 초기화해야합니다. 이 루프를 차단하는 상태이므로

그리고이 while 루프

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

포인터 temp

NULL는 동일하다. 따라서이 문장은 정의되지 않은 동작을 낳습니다.

    temp->next = ptr; 

결과는 정의되지 않았습니다.

이중 연결 목록이있는 경우 데이터 구성원 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; 
     } 
    } 

볼 수 있습니다이 경우

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

을 포함해야하는 것은 시범 프로그램을이다

#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 LOL을 가지고있다 – aashman

+0

@aashman 전혀. 천만에요. –

1

head을 설정하지 않았지만 액세스 할 수 있습니다. 이것은 그것이 초기화되지 않았으며 이것이 UB임을 의미합니다.

매개 변수없이 호출 한 경우에만 2 개의 ctors가 있고 head을 초기화합니다.

+0

'head'가 생성자에 설정되었습니다. – user4581301

+0

두 번째 생성자의 시작 부분에서 head를 NULL로 설정했지만 seg 오류가 여전히 존재합니다. – aashman

+0

'head'가'NULL'이라면, 여전히'temp = head'를하고'temp-> next = ptr'를'NULL'' temp'로합니다. Dereferencing null 포인터는 정의되지 않은 동작입니다. –