2014-11-22 34 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