2014-11-22 5 views
0

아래 링크 된 목록을 만든 다음 삭제하려고 시도하지만 프로그램이 컴파일되고 실행되지만 링크 된 목록은 나를 위해 삭제되지 않고 프로그램이 루프 등으로 멈추게됩니다 (기본적으로 수동으로 죽여야 만 할 때 종료되지 않습니다)C에서 연결된 목록 삭제

누구나 내가 'deletePoly'함수를 작성하기 전에 잘못 가고 있다고 제안 할 수 있습니까? (예 : 프로그램이 컴파일되어 실행될 수 있음) 괜찮아 지지만 100 회 대구를 통과했는데 문제가 무엇인지 알 수없는 것 같습니다. 여기

내 코드입니다 :

#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 showPoly(poly *);      
void deletePoly(poly *);      


int main(void) { 


    int a; 

    for(a = 0; a < MEMORY_SIZE; a++) 
    { 
     polyArray[a] = NULL;//Initialise each element of the array of pointers to NULL 
    }//end for 

     createPoly(&polyArray[0]); 
     showPoly(polyArray[0]); 

     srand(time(NULL)); 
     createPoly(&polyArray[1]); 
     showPoly(polyArray[1]); 

     showPoly(polyArray[0]); 

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

      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; 
    } 

}//end function createPoly 

void deletePoly(poly *node) { 

    poly *temp;//Create pointer to poly called 'temp' 

    while(node->next != NULL); 
    { 
    temp = node->next;//Assign the address of the next node to 'temp' 
    free(node);//Delete the current node 
    node = temp;//Assign the address of the next node in the list to 'node' 
    }//end while 

    node = NULL;//Set pointer 'node' to NULL to prevent a "lingering" pointer 

}//end function 'deletePoly' 


void showPoly(poly * node) { 

    while(node->next != NULL) { 

    if(node->coeff == 0) 
    { 
     node = node->next; 
    } 
    else if(node->coeff == 1 && node->pow > 1) 
    { 
     printf("[x^%i]", node->pow); 
     node = node->next; 
    } 
    else if(node->coeff == 1 && node->pow == 1) 
    { 
     printf("[x]"); 
     node = node->next; 
    } 
    else if(node->coeff != 0 && node->pow == 0) 
    { 
     printf("(%.2lf)", node->coeff); 
     node = node->next; 
    } 
    else if(node->pow == 0 && node->coeff == 0) 
    { 
     node = node->next; 
    } 
    else if(node->coeff != 1 && node->pow > 1 && node->coeff != 0) 
    { 
     printf("(%.2lf)[x^%i]", node->coeff, node->pow); 
     node = node->next; 
    } 
    else if(node->coeff != 1 && node->pow == 1 && node->coeff != 0) 
    { 
     printf("(%.2lf)[x]", node->coeff);// no need to print x to the power 0 
     node = node->next; 
    } 

    if(node->next != NULL) 
    { 
     printf(" + "); 
    } 
    } 
}//end function showPoly 
+0

'동안 (! 노드 -> 다음 = NULL);'->'동안 (노드 = NULL)'과'node = NULL'는 아무 효과가 없습니다. – BLUEPIXY

답변

2

당신이 있어야 할 코드를 삭제합니다!

void deletePoly(poly* node) 
{ 
    poly* next; 

    while (node != NULL) 
    { 
     next = node->next; 
     free(node); 
     node = next; 
    } 
} 
+0

typedef를 사용하여 해당 유형의 이름을 poly로 변경하면 구조 노드를 작성해야하는 이유는 무엇입니까? – SlamDunkMonk

+0

@ user4225083 예 일단 형식 변환하면 폴리를 사용할 수 있습니다. 나는 여기에 일반적으로 사용되고 있었다. – Gopi

+0

좋아, 고마워. 당신은 (node! = NULL) 왜 이것을 가지고 있습니까? 내 방식에 대한 나의 이해는 다음 노드라는 각 노드의 멤버에 액세스하여 다른 노드를 가리키는 지 여부를 확인하는 것입니다. 분명히 작동하지 않습니다. 귀하의 방식에 대한 나의 이해는 while 본문의 첫 번째 줄에서 다음 노드의 주소를 포인터 변수 next에 할당 한 다음 현재 노드를 해제하고 다음 노드 (즉, 다음)에 포인터를 할당한다는 것입니다. 이제 느린 포인터 변수 'node'와 물론이 값이 null이면 while 루프가 종료됩니다. 이 올바른지? – SlamDunkMonk