2014-11-22 125 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' – 2014-11-22 02:02:00

+0

好,谢谢,我假设它在poly中的while循环内,我是否正确? – SlamDunkMonk 2014-11-22 02:04:08

+0

你不检查'if(!node)'。 – EOF 2014-11-22 02:07:00

回答

0

据我了解,在main功能只创造了第一个多项式(poly[0]),但你试图将它们全部删除(循环中的主要功能发生最高为MEMORY_SIZE)。

你也应该初始化所有的指针为NULL开始执行程序(这是在C程序中的一个重要特征)之前,改变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循环,因为我只处理polyArray [0]中的元素,并且可以明确地做到这一点,但后来我将填充这个数组,并且我想要一种方式来最后清除它的程序。我会更改代码并尝试运行它,谢谢。 – SlamDunkMonk 2014-11-22 02:10:47

+0

是的,但你没有初始化变量,也没有使用元素1到14.尝试调试你的程序,我很确定当ddeletePoly在循环中调用时,在main函数中count = 1时会引起错误(对不起 - 在这台计算机上没有调试器,我正在做的事情在我的脑海中) – rlinden 2014-11-22 02:13:55

+0

只是不要忘记初始化部分:for(count = 0; count rlinden 2014-11-22 02:14:51