c - 释放链表中的节点

标签 c list pointers free

我正在用 C 中的链表做一些练习。遇到了指针问题。我正在尝试实现函数 RemoveListByIndex(list, index)。 到目前为止,我这段代码。

void RemoveNodeByIndex(struct node** head,int index) // NOT WORKING YET
{
  // Remove node list
  struct node* current = GetNodeByIndex(*(&head), index);
  struct node* prev    = GetNodeByIndex(*(&head), index-1);

  // For debugging
  PrintHRLine();
  printf("Deleting item %d from list.\n", index);
  printf("Item %d data = %d\n", index, current->data);
  PrintHRLine();
  // Change link from prev link node to the next
  prev->link = current->link;

  // Unlink node wished to delete
  current->link = NULL;
  current->data = 0;

  // Free data
  free((struct node*) current);
}

GetNodeByIndex 函数:

struct node* GetNodeByIndex(struct node** list, int index)
{
  // Return node by an index given
  // if error it returns NULL
  // for i < index
  //       next_node <- node.link
  //     node <- next.node
  // return next_node

  struct node* current = *list;
  int counter = 0;

  if (current)
    {
      while (current->link != NULL)
    {
      if (counter == index)
        return current;

      current = current->link;
      counter++;
    }
    }
  else
    {
      return NULL;
    }

  return NULL;
}

我的列表结构如下:

struct node
{
  int data;
  struct node* link;
};

然后我从我的主代码中调用函数,如下所示:

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

  int i;
  edit_node = (struct node*)malloc(sizeof(struct node));

  // Create a list of 10 elements
  for (i = 0; i < SIZE_OF_LIST; i++)
    {
      struct node* item = (struct node*) malloc(sizeof(struct node));
      item->data = i;
      if (i==0)
        head = item;
      printf("(item+%d)->data = %d\n",i,i);
      if (i<SIZE_OF_LIST-1)
    {
      item->link = (item+i+1);
      printf("(item+%d->link = (item+%d+1);\n", i, i);
      printf("(item+%d adress = 0x%lx\n",i, (unsigned long int) &item+i);
    }
      else 
    {
      item->link = NULL;
      printf("(item+%d->link = NULL;\n", i);
    }
}

  // RemoveNodeByIndex(head, 5);
  // PrintListData(&head);
  edit_node->data = 1001;
  AddLastNode(&head, &edit_node);
  SearchNode(&head, 101);
  RemoveNodeByIndex(&head, 8);
  PrintListData(&head);

  // Free memory
  free(item);
  free(edit_node);
  free(head);

  return 0;
}

一切似乎都很好,除了当我进行 free() 调用时。它失败。 输出:

===================================
Deleting item 8 from list. Located : 0x7fffd20ecad0
Item 8 data = 6
===================================
*** Error in `./a.out': double free or corruption (out): 0x00000000011b2090 ***
Aborted (core dumped)

我怀疑我处理指针的方式有误。 但是我做错了什么?

编辑: 我已将所有源代码包含在以下链接中:SOURCE CODE 我在这里包含了程序生成的所有输出:OUTPUT

提前致谢。

最佳答案

在您的“RemoveNodeByIndex”函数中,请检查以下行。

struct node* current = GetNodeByIndex(*(&head), index);
struct node* prev    = GetNodeByIndex(*(&head), index);

两者将具有相同的节点,并且您将当前节点设置为 NULL,如果当前节点被删除,则不会保留任何链接。这可能会导致未定义的行为。

关于c - 释放链表中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772565/

相关文章:

c++ - 如何保存一个指针地址,以便另一个指针可以继续工作?

c++ - 指针、智能指针还是共享指针?

c++ - 0x0000FFFFFFFFFFFFLL 是做什么的

C程序: implicit declaration of function and conflicting types

c - 开关盒 0 : entering 0 and entering a letter

java - 在 Java 中存储未知数量的字符串的最快方法是什么?

c - 如何修改这个字符串压缩C代码来处理超过9次的字符重复?

ios - 滚动后 SwiftUI 列表单元格未正确显示选择状态

java - 将列表带入内存的成本

C 理解指针数组的问题