簡體   English   中英

如何從雙向鏈表中刪除特定值?

[英]How to delete a specific value from a doubly linked list?

我被分配了一個帶有雙重鏈接列表的任務,以從列表中刪除特定數字。 我的代碼給出了訪問沖突錯誤。 即使經過多次試運行,我也無法弄清楚出了什么問題。 該任務基本上是創建一個搜索 function 以在鏈表中找到特定數字,以及刪除 function 刪除該特定鏈接。

node* search(int val){
    node* cur=head;
    while(cur!=NULL){
        if(cur->data==val){
            cout<<"value found "<<val<<endl;
            return cur;
        }
        cur=cur->next;
    }
    cout<<"value not exist"<<endl;
    return NULL;
}

bool delspval(int val){
    node*temp=0;
    if(search(val)==NULL){
        return 0;
    }
    else{
        temp=search(val);
        temp->prev->next=temp->next;
        delete temp;
        temp=0;
        cout<<"specific value "<<val<<" deleted"<<endl;
        return 1;
    }
}

在上面給出的代碼中,行temp->prev->next=temp->next; 給出錯誤。 我幾乎是鏈表的初學者,所以任何幫助將不勝感激。

最小的工作代碼:

#include<iostream>
using namespace std;
class dll{
    struct node{
        int data;
        node *next,*prev;
    };
    node *head;
public:
    dll(){
        head=NULL;
    }
    void inatst(int val){
        node *temp=new node;
        temp->data=val;
        temp->next=head;
        head=temp;
    }
    node* search(int val){
        node* cur=head;
        while(cur!=NULL){
            if(cur->data==val){
                cout<<"value found "<<val<<endl;
                return cur;
            }
            cur=cur->next;
        }
        cout<<"value not exist"<<endl;
                    return NULL;
    }
    bool delspval(int val){
        node*temp=0;
        if(search(val)==NULL){
            return 0;
        }
        else{
            temp=search(val);
            temp->prev->next=temp->next;
            delete temp;
            temp=0;
            cout<<"specific value "<<val<<" deleted"<<endl;
            return 1;
                }
            }
    void display(){
        node*cur=head;
        while(cur!=NULL){
            cout<<cur->data<<" ";
            cur=cur->next;
        }
        cout<<endl;
    }
    ~dll(){
        while(head!=NULL){
            node*cur=head;
            head=cur->next;
            delete cur;
            cur=head;
        }
    }
};
void main(){
    dll l1;
    l1.inatst(1);
    l1.inatst(2);
    l1.inatst(3);
    l1.inatst(4);
    l1.inatst(5);
    l1.inatst(6);
    l1.display();
    l1.delspval(3);
    system("pause");
}

對於初學者, search() function 在delspval() function 中被調用兩次:

if(search(val)==NULL){

temp=search(val);

這使得delspval() function 效率降低。

這個說法:

temp->next->prev=temp->next;

沒有意義。

delspval() function 可以通過以下方式定義。 我想 class 只包含一個指向head節點的指針。 如果 class 還包含指向tail節點的指針,則必須修改下面的 function。

bool delspval( int val )
{
    node *temp = search( val );
    bool success = temp != nullptr;

    if ( success )
    {
        if ( temp->next != nullptr )
        {
            temp->next->prev = temp->prev;
        }
        // If the class has a pointer to the tail node
        //   then uncomment the else part  
        /*
        else
        {
            tail = temp->prev;
        }
        */

        if ( temp->prev != nullptr )
        {
            temp->prev->next = temp->next;
        }
        else
        {
            head = temp->next;
        }

        delete temp;
    }

    return success;
}

你絕對不應該調用 search 兩次。 您基本上將運行時間加倍。

但是如果找到該值的解決方案:如果tmp->next不是null那么它指向tmp,但它現在需要tmp之前的項目。 如果 tmp->prev 不是,則它指向 tmp,但它需要指向 tmp 之后的項目。

最后,如果 tmp = head 那么 head 必須更改為 tmp 之后的項目。

Tmp = search()
If tmp == null then return
Prev = tmp->prev
Next = tmp->next
If prev ≠null then prev->next = next
If next ≠ null then next->prev = prev
If head = tmp then head = next. 
Delete tmp. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM