链接列表无法连接节点

时间:2014-05-28 20:18:50

标签: c list

我创建了一个列表,如果节点已连接,我会通过countnodes函数进行检查。 countnodes函数给了我正确的答案,我认为一切都没问题。但是,当我试图删除一个节点时,我意识到节点甚至没有连接到头部。我知道问题出在insert函数中,因为cur总是给出一些不同于零的东西,insert函数返回零,节点永远不会相互连接。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    int datum;


    struct node *next;
};

struct node *search(struct node *head, int id, struct node **prev)
{
    struct node *tmp = 0;
    tmp = (struct node*)malloc(sizeof(struct node));
    *prev = (struct node*)malloc(sizeof(struct node));

    tmp = head;
    printf("\n\ntmp->datum = %d",tmp->datum);

    while(tmp!=NULL && tmp->datum < id)
    {
        *prev = tmp;
        tmp = tmp->next;


    }

    if(tmp==NULL || tmp->datum != id)
    {

         return NULL;
    }


    return tmp;
};


int insert(struct node **H, struct node *tmp)
{
    struct node *cur = 0 , *prev = 0;
   // tmp = (struct node*)malloc(sizeof(struct node));
    cur = (struct node*)malloc(sizeof(struct node));

    printf("\n\ninsert datum = %d\n\n",tmp->datum);
        cur = search(*H,tmp->datum,&prev);



    if(cur) return 0;
    printf("\nox\n");

    if(prev==NULL)
    {
        printf("\nNULL\n");
        tmp->next = *H;
        *H = &tmp;
    }
    else
    {
        printf("\nELSE\n");
        tmp->next = (prev->next);
        prev->next = tmp;
    }

    return 1;

}

int delete(struct node **h,int price)
{
    struct node *cur, *prev;
    cur = (struct node*)malloc(sizeof(struct node));
    cur = search(*h,price,&prev);

    if(!cur) return 0;

    if(prev)
    {
        prev->next = cur->next;
        free(cur);
        printf("\n\nsimple delete\n\n");
    }
    else
    {
        *h = cur->next;
         free(cur);
         printf("\n\nhead delete\n\n");
    }

    return 1;
}

int countnodes(struct node *h)
{
    int n=0;
    struct node *tmp = 0;
    tmp = (struct node*)malloc(sizeof(struct node));
    tmp = h;
    while(tmp!=NULL)
    {
        n++;
        printf("\n\ndatum = %d\n",tmp->datum);
        tmp = tmp->next;



    }

    printf("\n\n\nNodes = %d\n",n);
    return n;

}

int main()
{
    struct node *head;
    struct node  *cur;
    int i=0;

    head = (struct node*)malloc(sizeof(struct node));
    cur = (struct node*)malloc(sizeof(struct node));

    head->datum = i;
    head->next = NULL;

    cur = head;

    for(i=1;i<5;i++)
    {

        cur->next = malloc(sizeof(struct node));

        insert(&head,cur);
        cur = cur->next;
        cur->datum = i;
        cur->next = 0;


    }



        delete(&head,0);
        //countnodes(head);



    return 0;
}

2 个答案:

答案 0 :(得分:2)

我在您的代码中看到的问题:

  1. search

    tmp = (struct node*)malloc(sizeof(struct node));
    *prev = (struct node*)malloc(sizeof(struct node));
    

    这些是不必要的malloc。它们也是内存泄漏。将它们更改为:

    *prev = NULL;
    
  2. insert

    *H = &tmp;
    

    这条线错了。双方是不同的指针类型。也许这是一个错字。它必须是:

    *H = tmp;
    
  3. delete

    cur = (struct node*)malloc(sizeof(struct node));
    

    这也是不必要的malloc和内存泄漏。

  4. countnodes

    tmp = (struct node*)malloc(sizeof(struct node));
    

    这也是不必要的malloc和内存泄漏。

  5. main

    cur = (struct node*)malloc(sizeof(struct node));
    

    这也是一个不必要的malloc和内存泄漏。

    此外,以下行不会用于任何目的。

    cur = head;
    

    for循环中,您有:

    cur->next = malloc(sizeof(struct node));
    insert(&head,cur);
    cur = cur->next;
    cur->datum = i;
    cur->next = 0;
    

    我认为您要做的是添加datumi的节点。但是,在节点上设置数据之前,您正在调用insert。你需要的是:

    cur = malloc(sizeof(struct node));
    cur->datum = i;
    cur->next = 0;
    insert(&head,cur);
    
  6. 希望我没有错过任何东西。

    <强>更新

    我错过了另一个不必要的malloc和内存泄漏。您不需要insert中的以下行。

    cur = (struct node*)malloc(sizeof(struct node));
    

答案 1 :(得分:0)

如几条评论所示,问题代码揭示了对malloc()和指针的不正确理解。

下面是一个问题代码的表示,其中包含一个malloc()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
   {
   int datum;
   struct node *next;
   };

struct node *search(struct node *cur, int id, struct node **prev)
   {
   while(cur && cur->datum < id)
      {
      *prev = cur;
      cur = cur->next;
      }

   if(cur)
      if(cur->datum != id)
         cur=NULL;

   return(cur);
   };

int delete(struct node **head, int price)
   {
   struct node *cur, *prev=NULL;

   cur = search(*head, price, &prev);
   if(NULL == cur)
      return 0;

   if(prev)
      {
      prev->next = cur->next;
      free(cur);
      }
   else
      {
      *head = cur->next;
      free(cur);
      }

   return 1;
   }

int countnodes(struct node *tmp)
   {
   int n=0;

   for(;tmp; tmp = tmp->next)
      n++;

   return(n);
   }

int insert(struct node **head, struct node *new)
   {
   struct node *cur = NULL;
   struct node *prev = NULL;

   if(*head)
      {
      cur = search(*head, new->datum, &prev);
      if(cur)
         return(0);
      }      

   if(prev)
      {
      new->next = prev->next;
      prev->next = new;
      }
   else
      {
      new->next = *head;
      *head = new;
      }

   return(1);
   }

int main()
   {
   struct node *head = NULL;
   int i;

   for(i=0;i<5;i++)
      {
      struct node *new = malloc(sizeof(*new));
      if(NULL == new)
         {
         fprintf(stderr, "malloc() failed\n");
         exit(1);
         }

      new->datum = i;
      new->next = NULL;
      printf("insert datum = %d\n", new->datum);
      insert(&head, new);
      }

   delete(&head,0);
   printf("countnodes = %d\n", countnodes(head));

   return 0;
   }