【问题标题】:Deleting a node by its address only but it deletes other value also and how to prevent memory leak仅按地址删除节点,但也会删除其他值以及如何防止内存泄漏
【发布时间】:2020-10-31 12:23:00
【问题描述】:

我编写了一个仅通过地址删除节点的代码,函数deleteNode 正在删除该位置的节点,但它正在删除更多值,我无法删除最后一个节点。

#include<bits/stdc++.h>
using namespace std;

struct Node {                                   //node defination
  int data;
  struct Node *next;
  Node(int x) {
    data = x;
    next = NULL;
  }
}*head;

Node *findNode(Node* head, int search_for)     //find the node that will delete
{
    Node* current = head;
    while (current != NULL)
    {
        if (current->data == search_for)
            break;
        current = current->next;
    }
    return current;
}


void insert()                                 //insert the values in node
{
    int n,i,value;
    Node *temp;
    scanf("%d",&n);

    for(i=0; i<n; i++)
    {
        scanf("%d",&value);
        if(i==0)
        {
            head=new Node(value);
            temp=head;
            continue;
        }
        else
        {
            temp->next= new Node(value);
            temp=temp->next;
            temp->next=NULL;
        }
    }
}

void printList(Node *node)                   //print the value in node
{
    while (node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
    cout << endl;
}
void deleteNode(Node *node_ptr);

int main(void)                              //main starts from here
{

    int k,n,value;

    {
        insert();
        scanf("%d",&k);
        Node *del = findNode(head, k);
        if (del != NULL && del->next != NULL)
        {
            deleteNode(del);
        }
        printList(head);
    }
    return(0);
}
void deleteNode(Node *pos)                  //delete node function
{
   struct Node *temp;
   while(temp->next!=0)
   {
       temp=pos->next;
       pos->data=temp->data;
       pos->next=temp->next;
       pos=temp;
   }
 }

输入

5(链表的大小)

1 2 3 4 5(列表元素)

2(要删除的位置)

预期输出

1 3 4 5

电流输出

1 3 5

【问题讨论】:

  • 预期输出中的“6”来自哪里?
  • deleteNodetemp 不应该从head 开始吗?使用调试器单步调试您的代码,您会发现错误。
  • struct Node *temp; while(temp-&gt;next!=0) 调用 undefined behavor 以使用未初始化的非静态局部变量的(不确定)值。
  • 请阅读Why should I not #include <bits/stdc++.h>?Why is “using namespace std;” considered bad practice? 并停止使用在线竞赛网站作为学习资源。让some good books 改为阅读,或者最好参加一些课程。
  • struct Node *temp; 请注意,在 c++ 中 Node *temp; 就足够了。在此之前您不需要结构。

标签: c++ linked-list


【解决方案1】:

创建析构函数:

struct Node {                                   //node defination
  int data;
  struct Node *next;
  Node(int x) {
    data = x;
    next = NULL;
  }
  ~Node() {    // destructor
    if (next)
      delete next;
  }
}*head;

所以在删除的时候,所有的节点都会被删除。

deleteNode 函数不需要遍历列表:

void deleteNode(Node *pos)                  //delete node function
{
   struct Node *temp;

   if(pos->next != 0)
   {
       temp = pos->next;
       pos->data = temp->data;
       pos->next = temp->next;
       temp->next = nullptr;  // <- set next to null to avoid next nodes deletion
       delete temp;  // and delete
   }
}

main 中删除头部:

if (head)
    delete head;

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多