繁体   English   中英

删除单链列表中的节点

[英]deletion of a node in a singly linked list

我用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;
}

程序的输出。

int del_node(int val_del) {
    node* temp_del = head;
    if(head == nullptr) {
        cout<<"no element to delete.!";
        exit(0);
    } else if(head->data == val_del) {
         // If head is to be deleted
         head = head->next;
    } 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; This is wrong deletion 
            }
         temp_del = temp_del->next;
        }
    }
    // delete the node if one found else not
    if (temp_del != nullptr)
        delete temp_del;

    return 0;  // This should return true or false, do check what you want as return type
}

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)将导致false,即不会执行循环。 总体结果是该代码不执行任何操作。

现在是数字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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM