2012-05-18 2 views
0

int 값의 연결된 목록을 처리하려고합니다. 3 int 값을 목록에 추가하고 인쇄하지만 3 문제를 인쇄 한 후 문제가 발생하면 프로그램이 인쇄 기능을 사용하여 4 번째 줄을 인쇄합니다 (tempNode! = NULL)는 true를 나타내므로 인쇄 후 NULL이어야합니다. 3 값, 그래서 내게 인쇄 방법에 액세스 위반 읽기 오류를 제공합니다 cout << "index " << size-- << ", value: "<< tempNode->num << endl;액세스 위반 위치 읽기 : 연결된 목록 C++

그것은 내 목록 노드를 넘어 가고 있지만 어디 잘못 생각하고있다.

도와주세요, 2 일간이 문제를 파악하려고합니다.

코드입니다.

IntList::IntList() 
{ 
    first = NULL; 
    last = NULL; 
    size = 0; 
} 


IntList::Node::Node(const int& info, Node* next = NULL) 
{ 
    num = info; 
    next = next; 
} 


IntList::~IntList() 
{ 
    Node* tempNode = first; 
    while (tempNode != NULL) 
    //for(int i = 0; i < size; i++) 
    { 
     Node* nextNode = tempNode->next; 
     delete tempNode; 
     tempNode = nextNode; 
    } 

    first = last = NULL; 
    size = 0; 
    } 


IntList::IntList(const IntList& wl) 
{ 
    cout << "here word list copy conts " << endl; 

    first = last = NULL; 
    size = wl.size; 
    if(wl.first != NULL){ 

     Node* tempNode = wl.first; 
     for(int i = 0; i < wl.size; i++) 
     { 
       addLast(tempNode->num); 
       tempNode = tempNode->next; 
     } 
    } 
} 


IntList& IntList::operator = (const IntList& wl) 
{ 
    cout << "here word list =" << endl; 
    if(this == &wl) 
     return *this; 

    Node* tempNode = first; 
    while (tempNode != NULL) 
    { 
     Node* nextNode = tempNode->next; 
     delete tempNode; 
     tempNode = nextNode; 
    } 

    first = NULL; 
    last = NULL; 

    if(wl.first != NULL) 
    { 
     for(int i = 0; i < wl.size; i++) 
     { 
      addLast(tempNode->num); 
      tempNode = tempNode->next; 
      size++; 
    } 
} 

return *this; 
} 

void IntList::addFirst(int& winfo) 
{ 
    Node* firstNode = new Node(winfo); 
    //Node firstNode(winfo); 
    if(first == NULL) 
    { 
     first = last = firstNode; 
    } 
    else 
    { 
     firstNode->next = first; 
     first = firstNode; 
    } 
    //increment list size 
    size++; 
} 

void IntList::print(ostream& out) 
{ 
    Node* tempNode = first; 
    while (tempNode != NULL) 
    { 
     out << "\t"; 
     cout << "index " << size-- << ", value: "<< tempNode->num << endl; 
     tempNode = tempNode->next; 
    } 
} 
+0

문제를 설명하는 * minimal *이지만 완전한 예제로 줄일 수 있습니까? 우리는 그것을 수정하지 않고 컴파일하고 실행할 수 있습니다. –

+0

"addLast"는 어디에 있습니까? – Joe

+0

헤더 파일을 제공 할 수 있습니까? – newbieLinuxCpp

답변

2

Node 생성자의 next 파라미터는 next 부재 그림자 때문에 next = next 파라미터에 할당된다.
둘 중 하나의 이름을 변경하십시오.

또한 인쇄 중에 size을 수정하지 마십시오.

+0

감사합니다. 오류 메시지없이 작동합니다. – newbieLinuxCpp

관련 문제