2014-11-22 3 views
2

링크 된 목록의 모든 노드를 삭제하려고하는데 세그멘테이션 오류가 발생합니다.링크 된 목록을 삭제할 때의 세그먼트 오류

처음에는 작동하는 코드가 있었지만 목록의 첫 번째 노드 만 삭제했지만 모든 노드를 삭제하고 모든 포인터 중복 포인터를 삭제하려고합니다.

또한 여러분 중 일부가 연결된 목록을 작성하는 데 사용하고있는 기능을 확인하고 그것이 좋다고 생각하는지 또는 개선이 이루어 졌는지에 대한 피드백을 주시면 감사하겠습니다.

감사합니다. 여기

코드입니다 :

#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 

#define MEMORY_SIZE (15) 


typedef struct link { 
    double coeff; 
    int pow; 
    struct link * next; 
} poly; 

poly *polyArray[MEMORY_SIZE];// array of 15 polynomials to play with 

// /** The function prototypes */ 
void createPoly(poly **);     
void deletePoly(poly *);      

/** 
* The main function 
*/ 
int main(void) { 

    printf("\n\n\t***************************************************"); 
/* printf("\n\tDemonstrating Polynomial Creation"); 
    printf("\n\t***************************************************");*/  
     printf("\n\t1st polynomial\t"); 
     createPoly(&polyArray[0]); 
     showPoly(polyArray[0]); 
    srand(time(NULL)); 
//  printf("\n\n\tCreating and storing the 2nd polynomial\n"); 
// createPoly(&polyArray[1]); 
// showPoly(polyArray[1]); 



    showPoly(polyArray[0]); 
    printf("\n\t***************************************************"); 
    printf("\n\tProgram has Ended, Deleting all polynomials"); 
    printf("\n\t***************************************************"); 

     int count; 
     for(count = 0; count < MEMORY_SIZE; count++) 
    { 
     deletePoly(polyArray[count]); 
    } 


    printf("\n\n"); 

    showPoly(polyArray[0]); 
    return 0; 
}//end main function 


////////////////////////////////////////////////////////////////////////////////////// 

void createPoly(poly **node) { 

    poly *tempnode; //To hold the temporary last address 
    tempnode = (poly*)malloc(sizeof(poly)); //create the first node 
    *node = tempnode; //Store the head address to the reference variable 

    int flag = 1 + rand()%3;; // A flag to control the number of terms 
    int counter; 

    for(counter = 0; counter <= flag; counter++) 
    { 
      tempnode->pow = (flag-counter); 
     tempnode->coeff = ((double)(rand()%20))/((double)(1 + rand()%20)); 

     if((counter < flag) && (counter >= 0) ) 
     { 
      tempnode->next = (poly*)malloc(sizeof(poly)); //Grow the list 
     } 
     else if (counter == flag) 
     { 
      tempnode->next = NULL; 
     } 

     tempnode = tempnode->next; 
    } 

} 

void deletePoly(poly *node) { 

    poly *temp; 

    if(node->next == NULL) 
    { 
     free(node); 
     node = NULL; 
    } 
    else 
    { 
     while(node->next != NULL) 
    { 
     temp = node->next; 
     free(node); 
     node = temp; 
    }//end while 
     node = NULL; 
    }//end 'if/else' 

}//end function 'deletePoly' 
+0

버그가 deletePoly' –

+0

확인 감사'에서입니다 , 나는 그것이 폴리의 while 루프 안에 있다고 가정하고 있습니다. 맞습니까? – SlamDunkMonk

+0

당신은'if (! node)'를 확인하지 않을 것입니다. – EOF

답변

0

내가 알고있는 것처럼, main 기능은 첫 번째 다항식 (poly[0])을 생성한다, 그러나 당신은 주요 기능이가는 그들에게 모든 (루프를 삭제하려고 최대 MEMORY_SIZE까지).

또한 프로그램을 (이 C 프로그램의 중요한 기능입니다) 시작하기 전에 NULL로 모든 포인터를 초기화하고 deletePoly 이런 식으로 변경해야합니다 :

void deletePoly(poly *node) {  
    poly *temp;  
    while(node != NULL) { 
     temp = node->next; 
     free(node); 
     node = temp; 
    }//end while 
    node = NULL;  

}//end function 'deletePoly' 
+0

그렇습니다. 포인터 'polyArray'의 배열은 15 개의 요소를 가질 수 있습니다. 그리고 배열의 인덱스 [0]을 통해 첫 번째 다항식 (즉, 링크 된리스트)을 가리키고 싶습니다. 내가 MEMORY_SIZE로 올라가는 main의 루프는 polyArray [0]의 요소만을 다루고 있으므로 명시 적으로 할 수 있었지만 나중에이 배열을 채울 것이므로이 인스턴스에서는 필요하지 않습니다. 그것을 프로그램 끝에서 끝내십시오. 코드를 변경하고 실행 해 봅니다. 감사합니다. – SlamDunkMonk

+0

네, 그렇지만 변수를 초기화하지 않았고 1에서 14까지의 요소를 사용하지 않았습니다. 프로그램을 디버깅 해보고 ddeletePoly가 루프에서 호출 될 때 오류가 발생할 것이라고 확신합니다. 주 기능에서 count = 1 일 때 (미안합니다. -이 컴퓨터에 디버거 없음. 내 머리 속에서 일을하고있다.) – rlinden

+0

모든 것이 시작되기 전에 for (count = 0; count rlinden

관련 문제