링크 된 목록의 모든 노드를 삭제하려고하는데 세그멘테이션 오류가 발생합니다.링크 된 목록을 삭제할 때의 세그먼트 오류
처음에는 작동하는 코드가 있었지만 목록의 첫 번째 노드 만 삭제했지만 모든 노드를 삭제하고 모든 포인터 중복 포인터를 삭제하려고합니다.
또한 여러분 중 일부가 연결된 목록을 작성하는 데 사용하고있는 기능을 확인하고 그것이 좋다고 생각하는지 또는 개선이 이루어 졌는지에 대한 피드백을 주시면 감사하겠습니다.
감사합니다. 여기
코드입니다 :#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'
버그가 deletePoly' –
확인 감사'에서입니다 , 나는 그것이 폴리의 while 루프 안에 있다고 가정하고 있습니다. 맞습니까? – SlamDunkMonk
당신은'if (! node)'를 확인하지 않을 것입니다. – EOF