节点的缺失单链表

问题描述 投票:0回答:2

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

c++ c++11
2个回答
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
}

0
投票

del_node功能有两个问题:

1)它不能删除节点时列表中恰好有1元

2)删除错误的元素

因此,让我们开始与1号和看代码:

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

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

一)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;
}
© www.soinside.com 2019 - 2024. All rights reserved.