2011-11-11 2 views
-1

다음 코드를 사용하여 링크 된 목록을 구현하려고하는데 세그먼트 오류가 어디서 발생했는지 알려주십시오.gcc에서 포인터로 인한 세그먼트 오류 발생

void display() 
{ 
    node *temp=n; 
    while(temp->nextnode==NULL) 
    { 
     cout<<temp->info; 

    } 
} 

가 있어야한다 :

나는 우분투 GCC 컴파일러를 사용하고,

#include<iostream> 

using std::cout; 
using std::cin; 


class ll 
{ 
    struct node 
    { 
     int info; 
     node *nextnode ; 
    }*n; 



public: 
    ll() 
    { 
     n=NULL; 
    } 

    void getinfo() 
    { 
     node *temp,*r; 


     if(n==NULL) 
     { 
      temp=new node; 
      cout<<" \n enter the first elements of linklist \n"; 
      int z; 
      cin>>z; 
      //i guess problem starts here 
      temp->info=z; 
      cout<<"the value of info is"; 
      temp->nextnode = NULL; 
      n=temp; 
     } 
     else{ 
      temp=n; 
      cout<<"heheh balls"; 
      while(temp->nextnode==NULL) 
      { 
       temp=temp->nextnode; 
      } 
      r=new node; 
      cout<<"enter the element \t"; 
      int y; 
      cin>>y; 
      r->info=y; 
      r->nextnode=NULL; 
      temp=r; 
     } 
    } 

    void display() 
    { 
     node *temp=n; 
     while(temp->nextnode==NULL) 
     { 
      cout<<temp->info; 

     } 
    } 

}; 

int main() 
{ 
    ll p; 
    int v; 
    cout<<"enter the number of elements to be added to linklist \t"; 
    cin>>v; 
    //tryn to input linklist from terminal 
    for(int i=0;i<v;i++) 
    { 
     p.getinfo(); 
    } 
    p.display(); 

    return 0; 
} 
+0

코드를 선택하고 Ctrl-K를 눌러 코드를 다시 포맷하십시오. – chill

+5

글쎄, GDB에서 실행하면 충돌이 발생한 줄과 충돌 할 때 모든 변수의 값을 알 수 있습니다. –

+0

OliCharlesworth에게 감사하지만 GDB는 어디서 구할 수 있습니까? g ++로 빌드하면됩니다. – Imposter

답변

2
while(temp->nextnode==NULL) 
{ 
    temp=temp->nextnode; 
} 
.... 
temp=r; 

while(temp->nextnode!=NULL) 
{ 
    temp=temp->nextnode; 
} 
.... 
temp->nextnode=r; 

동일해야합니다 내게 뭔가를 제안 해주십시오은 간다

void display() 
{ 
    node *temp=n; 
    while(temp!=NULL) 
    { 
     cout<<temp->info; 
     temp = temp->nextnode; 
    } 
} 
+0

감사합니다 @KoKoToru pls 코드 부분을 세그먼트 오류 – Imposter

+0

에 연결하면 0 점에 액세스 할 수 있습니다 .. 당신의 동안은 잘못 되었기 때문에 – KoKuToru

2

코드에서 몇 가지 오류가 있습니다, 아래에 보이는 :

다음 줄이 추가됩니다
void 
    getinfo() 
    { 
    node *temp, *r; 

    if (n == NULL) 
     { 
    temp = new node; 
    cout << " \n enter the first elements of linklist \n"; 
    int z; 
    cin >> z; 
    //i guess problem starts here 
    temp->info = z; 
    cout << "the value of info is"; 
    temp->nextnode = NULL; 

, 당신은 첫 번째 요소에 머리를 가리 키도록해야합니다.

n = temp; 

계속 ... 여기

 } 
    else 
     { 
    temp = n; 
    cout << "heheh balls"; 

는 다음 요소 여기 동안 반복한다는 !===을 변경했습니다.

while (temp->nextnode != NULL) 

계속 ...

 { 
     temp = temp->nextnode; 
     } 
    r = new node; 
    cout << "enter the element \t"; 
    int 
     y; 
    cin >> y; 
    r->info = y; 
    r->nextnode = NULL; 
    temp->nextnode = r; 
     } 
    } 

다음 기능 : 당신이 노드

while (temp) 
    { 
    cout << temp->info; 

그리고 돈있는 동안

은 다음 줄이 변경 ​​
void 
    display() 
    { 
    node *temp = n; 

, 인쇄 번호를 유지 다음으로 이동하는 것을 잊지 마라. 노드

 temp = temp->nextnode; 
     } 
    } 

그리고 그게 작동합니다.

+0

감사의 진정하지만 나는 "temp = new node; node * temp를 선언했을 때; 위의 "temp = new node" – Imposter

+0

@Imposter의 사용은 목록의 새 요소를 할당하기 만합니다. 'n == NULL'의 경우'n = new node;라고 간단하게 말할 수 있고'if'의 해당 분기에서 n을 사용합니다. 걱정에 대해 – chill

+0

고마움.그래서 "temp = new node;"라는 문장을 쓰지 않는다면 위의 코드에서 node * temp를 계속 수행하여 결과를 얻습니다. – Imposter