链接列表搜索功能修改列表

时间:2016-10-03 22:50:52

标签: c++ pointers linked-list doubly-linked-list

我正在尝试在C ++中实现双向链表,并且add函数正常工作但是find节点函数正在修改列表。 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;
};
};

2 个答案:

答案 0 :(得分:3)

start->data = d

第二个if块中的这一行将d分配给start-&gt;数据而不是比较两者。

答案 1 :(得分:2)

现在是了解const的好时机。

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) {

此代码将d的值分配给start->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;
    }

现在if会生成编译器错误。

清理后的代码版本可能如下所示:

#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