c - C中删除链表

标签 c pointers linked-list

下面我正在创建一个链表然后尝试删除它,但是程序将编译并运行,但是链表不会为我删除并且程序陷入循环或其他问题(基本上它不会终止什么时候我必须手动杀死它)

在编写“deletePoly”函数之前,任何人都可以建议我哪里出错了,一​​切都很好(即程序可以编译和运行)但我已经经历了 100 次 cod 但我不能似乎看出了问题所在。

这是我的代码:

#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

最佳答案

你删除的代码应该是:

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

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

关于c - C中删除链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27079090/

相关文章:

c - 如何在指针中保存多种类型的数据

java - Java删除重复的链表

c - 如何编写一个创建空队列的c函数?

Java awt 为什么我只能删除链表中最近添加的内容

c - 即使 GCC 优化关闭,是否也有必要使用 "volatile"限定符?

c++ - 如何使用 SWIG 在 C++ API 上生成 C 包装器?

c - 使用 openGL ES 1.1 渲染 .h Blender 导出到 iPhone

c++ - 初始化类的常量字符指针

c - 结构体内部指针的值

c++ - C++ 中的指针/C 字符串。如何过滤字符串?