删除单链列表中的节点

时间:2019-02-03 08:04:30

标签: c++ c++11

我已经用c ++编写了一个程序来删除单链列表中的节点,但是它不能按预期工作。我附上了输出图片,目的是为了更清楚地说明发生了什么错误。 代码:

int del_node(int val_del)                     //this section is producing error
    {
        node* temp_del=head;
        if(head==nullptr)
        {
            cout<<"no element to delete.!";
            exit(0);
        }
        else
        {
            while(temp_del->next!=nullptr)
            {
                if(temp_del->next->data==val_del)
                {
                    temp_del->next=temp_del->next->next;
                    delete temp_del->next->next;
                }
             temp_del=temp_del->next;
            }

        }
        return 0;
    }

这是一个类的功能。 这是完整的代码(如果有帮助的话):

#include<iostream>
using namespace std;

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

class linked_list
{
    node *head,*tail;
public:
    linked_list()
    {
        head=nullptr;
        tail=nullptr;
    }
    int create_last(int val_last)
    {
        node *temp=new node; if(!temp){cout<<"memory not allocated";    exit(1);}
        temp->data=val_last;
        temp->next=nullptr;
        if(head==nullptr)
        {
            head=temp;
            tail=temp;
        }
        else
        {
            tail->next=temp;
            tail=temp;
        }
        return 0;
    }

    int create_beg(int val_beg)
    {
        node *temp_head=nullptr;
        node *temp=new node; if(!temp){cout<<"memory not allocated";    exit(1);}
        temp->data=val_beg;
        temp->next=nullptr;
        if(head==nullptr)
        {
            head=temp;
            tail=temp;
        }
        else
        {
            temp_head=head;
            head=temp;
            temp->next=temp_head;
        }
        return 0;
    }

    int del_node(int val_del)                     //this section is producing error
    {
        node* temp_del=head;
        if(head==nullptr)
        {
            cout<<"no element to delete.!";
            exit(0);
        }
        else
        {
            while(temp_del->next!=nullptr)
            {
                if(temp_del->next->data==val_del)
                {
                    temp_del->next=temp_del->next->next;
                    delete temp_del->next->next;
                }
             temp_del=temp_del->next;
            }

        }
        return 0;
    }


    int show()
    {
        node* temp_show=head;
        while(temp_show!=nullptr)
        {
            cout<<temp_show->data<<"\n";
            temp_show=temp_show->next;
        }
        return 0;
    }

}info;

int main()
{
    int choice,ele; char cont;
    rep:
    cout<<"1. Insert node at the end\n";
    cout<<"2. Insert node at beg\n";
    cout<<"3. Delete node\n";
    cout<<"4. Show nodes\n";
    cout<<"5. Exit\n";
    cout<<"enter your choice: ";
    cin>>choice;
    switch(choice)
    {
        case 1: cout<<"Enter element:  ";
                cin>>ele;
                info.create_last(ele);
                break;
        case 2: cout<<"Enter element:  ";
                cin>>ele;
                info.create_beg(ele);
                break;
        case 3: cout<<"Enter element:  ";
                cin>>ele;
                info.del_node(ele);
                break;
        case 4: info.show();
                break;
        case 5: exit(0);
                break;
        default: cout<<"Wrong choice, Bye.!!";
                 exit(0);
    }
    cout<<"Do you want to continue(y/n): ";
    cin>>cont;
    if(cont=='y'||cont=='Y')
    {
        goto rep;
    }
    else
    {
        cout<<"thank you";
        exit(0);
    }
    return 0;
}

output of the program.

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
{% load user_tags %}
 {% load static %}

<html lang="en">
<head>

{% block extra_head %}{% endblock %}
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="{% static "user/images/favicon.ico"%}" type="image/x-icon">
    <title>Power Management</title>
    <!-- CSS -->
    <link rel="stylesheet" type="text/css" href="{% static 'user/css/app.css' %}">
    <style>
        .loader {
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: #F5F8FA;
            z-index: 9998;
            text-align: center;
        }

        .plane-container {
            position: absolute;
            top: 50%;
            left: 50%;
        }
    </style>
</head>
    <body class="light">
        {% block content %}
        {% endblock %}

         {% block grid  %}
        {% endblock %}

        {% block scripts %}

            <script src="{% static "user/js/app.js" %}"></script>
        {% endblock %}
        {% block extra_js %}

        {% endblock extra_js %}
    </body>
</html>

答案 1 :(得分:0)

del_node函数有两个问题:

1)当列表中只有1个元素时,无法删除该节点

2)删除错误的元素

所以让我们从数字1开始,看一下代码:

    if(head==nullptr)
    {
        cout<<"no element to delete.!";
        exit(0);
    }
    else
    {
        while(temp_del->next!=nullptr)

假设列表中包含恰好一个元素。这意味着:

a)head 不是 NULL

b)head->next,因此也temp_del->next NULL

因此while(temp_del->next!=nullptr)将导致错误,即将不执行循环。总体结果是该代码不执行任何操作。

现在输入2。代码为:

            if(temp_del->next->data==val_del)
            {
                temp_del->next=temp_del->next->next;   // You want to delete temp_del->next
                                                       // but here you change it

                delete temp_del->next->next;           // so here you delete the wrong node
                                                       // there is also an extra ->next
            }

您需要一个临时变量来保存指向要删除的节点的指针。

您可能希望代码为:

int del_node(int val_del)
{
    // Delete elements in the front
    while (head!=nullptr && head->data==val_del)
    {
        node* node_to_delete = head;
        head = head->next;
        delete node_to_delete;
        // return 0;  Uncomment if you only want one delete per function call
    }
    if(head==nullptr) return 0;

    // Delete elements not in the front
    node* temp_del=head;
    while(temp_del->next!=nullptr)
    {
        if (temp_del->next->data==val_del)
        {
            node* node_to_delete = temp_del->next;
            temp_del->next = temp_del->next->next;
            delete node_to_delete;
            // return 0;  Uncomment if you only want one delete per function call
        }
        temp_del=temp_del->next;
    }

    return 0;
}