2016-10-03 58 views
0

我想在C++中實現雙向鏈接列表,並且添加功能正常工作,但查找節點功能正在修改列表。 所有其他的函數,如insertAfter,delete都依賴於這個find函數,因此它們也沒有按預期工作。鏈接列表搜索功能修改列表

我是新來的C++,所以我不完全理解指針。我只是試圖在C++中複製我的Java程序。我確信在find函數中指向頭節點的指針會導致問題,但我不完全明白。

下面是我的代碼:

struct Node{ 
int data; 
Node* next; 
Node* prev; 


Node(int d) { 
    data = d; 
    }; 
}; 


struct DLL { 
    Node* head; 
    Node* tail; 
    int size; 

//Adding a Node to the Doubly LL 
void addNode(Node* n) { 
    //If LL is empty add the first Node 
    if (tail == NULL) { 
     tail = n; 
     head = n; 
    } 
    //Else add add node to the tail. Connect n to the tails next and make n the tail 
    else { 
     tail->next = n; 
     n->prev = tail; 
     tail = n; 
     tail->next = NULL; 
    } 

    size++; 
}; 

//Finding a random Node in the linked List 
//It will return the Node with the FIRST occurrence where data = d 
Node* findNode(int d) { 
//We will start at the head and then traverse through the entire list to find a Node where data = d 
    Node* start = head; 
    if (start == NULL) { 
     cout<<"No element in the List" <<endl; 
     return NULL; 
    } 
    // If head is the Node we are looking for 
    if (start->data = d) { 
     cout<< "Node found with matching data : " << start << endl; 
     return start; 
    } 
    //While next pointer is not null, traverse to search for a match.s 
    while (start->next != NULL) { 
     start = start->next; 
     if (start->data == d) { 
      cout<< "Node found with matching data : " << start << endl; 
      return start; 
     } 
    } 

    cout << "No node found with matching data = " << d <<endl; 
    return NULL; 
}; 
}; 
+0

解決這些問題的正確工具是你的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+1

找到此問題的正確工具是您的編譯器。注意**警告**! – Amit

+1

我建議更改DLL的名稱,因爲DLL通常與** d ** ynamic ** l ** inked ** l **庫相關聯。 –

回答

3
start->data = d 

這條線在你的第二個,如果塊被分配d開始 - >數據,而不是兩相比較。

2

這是學習const ness的好時機。

Node* findNode(int d) { 
//We will start at the head and then traverse through the entire list to find a Node where data = d 
    Node* start = head; 
    if (start == NULL) { 
     cout<<"No element in the List" <<endl; 
     return NULL; 
    } 
    // If head is the Node we are looking for 
    if (start->data = d) { 
     cout<< "Node found with matching data : " << start << endl; 
     return start; 
    } 

這個函數有寫訪問到列表中,並且不希望出現這種情況。不幸的是,你濫用其在過去的if語句這種訪問:

if (start->data = d) { 

該代碼分配的dstart->data,然後測試的值,如果分配給它的值不爲空。

我們可以標記該功能const容易:

//////////////////////vvvvv///////////////// 
Node* findNode(int d) const { 
//We will start at the head and then traverse through the entire list to find a Node where data = d 
    Node* start = head; 
    if (start == NULL) { 
     cout<<"No element in the List" <<endl; 
     return NULL; 
    } 
    // If head is the Node we are looking for 
    if (start->data = d) { 
     cout<< "Node found with matching data : " << start << endl; 
     return start; 
    } 

,現在如果將生成一個編譯器錯誤。

一個清理你的代碼的版本可能看起來像以下:

#include <iostream> 

struct Node { 
    int data_; 
    Node* next_ { nullptr }; 
    Node* prev_ { nullptr }; 

    Node(int data) : data_(data) {} 
}; 


struct DLL { 
    Node* head_ { nullptr }; 
    Node* tail_ { nullptr }; 
    int size_ { 0 }; 

    //Adding a Node to the Doubly LL 
    void addNode(Node* node) { 
     //If LL is empty add the first Node 
     if (tail_ == nullptr) { 
      tail_ = node; 
      head_ = node; 
      node->prev_ = node->next_ = nullptr; 
     } 
     //Else add add node to the tail. Connect n to the tails next and make n the tail 
     else { 
      tail_->next_ = node; 
      node->prev_ = tail_; 
      tail_ = node; 
      node->next_ = nullptr; 
     } 

     size_++; 
    } 

    //Finding a random Node in the linked List 
    //It will return the Node with the FIRST occurrence where data = d 
    Node* findNode(int data) const { 
     //We will start at the head and then traverse through the entire list to find a Node where data = d 
     //While next pointer is not null, traverse to search for a match.s 
     for (Node* start = head_; start != nullptr; start = start->next_) { 
      if (start->data_ == data) { 
       std::cout << "Node found with matching data : " << start << '\n'; 
       return start; 
      } 
     } 

     std::cout << "No node found with matching data = " << data << '\n'; 
     return nullptr; 
    } 
}; 

int main() 
{ 
    DLL dll; 

    Node n1(1), n3(3), n5(5); 
    dll.addNode(&n1); 
    dll.addNode(&n3); 
    dll.addNode(&n5); 

    if (dll.findNode(1) != &n1) 
     std::cerr << "wrong result for findNode(1)\n"; 
    if (dll.findNode(2) != nullptr) 
     std::cerr << "wrong result for findNode(2)\n"; 
    if (dll.findNode(3) != &n3) 
     std::cerr << "wrong result for findNode(3)\n"; 
    if (dll.findNode(4) != nullptr) 
     std::cerr << "wrong result for findNode(4)\n"; 
    if (dll.findNode(5) != &n5) 
     std::cerr << "wrong result for findNode(5)\n"; 
} 

現場演示:http://ideone.com/X34EgY