2014-11-22 103 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 2014-11-22 15:41:25

回答

2

您刪除代碼應該是:

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

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

當我使用typedef將該類型的名稱更改爲poly時,爲什麼我必須寫結構節點? – SlamDunkMonk 2014-11-22 16:59:58

+0

@ user4225083是的,一旦你被警告,你可以使用poly。我只是在這裏通用 – Gopi 2014-11-22 17:01:50

+0

好的,謝謝。你有(node!= NULL)這是爲什麼?我對自己的方式的理解是,我正在訪問名爲next的每個節點的成員,並且看看它是否指向另一個節點,顯然這不起作用。我對你的方式的理解是,在while body的第一行中,接下來將下一個節點的地址分配給指針變量,然後釋放當前節點並將指針指向下一個節點(即next)現在延時指針變量'節點',當然,如果這個值爲空,while循環將結束。它是否正確? – SlamDunkMonk 2014-11-22 17:22:55