c - 链表无法连接节点

标签 c list

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

#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;
}

最佳答案

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

  1. 搜索中:

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

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

    *prev = NULL;
    
  2. 中插入:

    *H = &tmp;
    

    这一行是错误的。两侧是不同的指针类型。也许这是一个错字。它需要是:

    *H = tmp;
    
  3. 删除中:

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

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

  4. 计数节点中:

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

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

  5. 中:

    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;
    

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

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

希望我没有错过任何东西。

更新

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

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

关于c - 链表无法连接节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921063/

相关文章:

c - 为什么 PRIx64 打印 "lx"而不是 16 个十六进制字符?

c# - 如何计算任务中的列表?

javascript - 如何存储 Monoidal List 功能链的数据?

c# - 如何将此示例 foreach 转换为 lambda 表达式?

java - 计算 List<Integer> 中出现的次数

c - 定义此C宏的正确方法是什么?

c - gcc 不会编译 SDL C 程序(对 SDL 函数的 undefined reference )

c - 为什么这个 char 驱动程序总是只复制一个字节?

从函数更改 char 数组

python - 创建 "A"和 "B"字母表的群体,每个字母表的频率为 50%