在单个链表中搜索特定值

时间:2018-05-22 03:43:33

标签: c++ xcode linked-list

我正在尝试实现通用的单个链表。到目前为止,我已经完成了所有事情,但我无法使我的搜索功能正常工作。它应该在输出中打印“是”但没有任何反应。

这是我的代码:

#ifndef LinkedList_hpp
#define LinkedList_hpp

#include <iostream>

template<class T>
struct Node {
    T data;
    Node<T>* next;
};

template<class T>
class SingleLinkedList {
private:
    Node<T>* head;
    Node<T>* tail;

public:
    SingleLinkedList() {
        head = nullptr;
        tail = nullptr;
    }

    void createNode(const T& theData) {
        Node<T>* temp = new Node<T>;
        temp->data = theData;
        temp->next = nullptr;
        if(head == nullptr) {
            head = temp;
            tail = temp;
            temp = nullptr;
        }
        else {
            tail->next = temp;
            tail = temp;
        }
    }

    void display() {
        Node<T>* temp = new Node<T>;
        temp = head;
        while(temp != nullptr) {
            std::cout << temp->data << "\t";
            temp = temp->next;
        }
    }

    void insert_start(const T& theData) {
        Node<T>* temp = new Node<T>;
        temp->data = theData;
        temp->next = head;
        head = temp;
    }

    void insert_position(int pos, const T& theData) {
        Node<T>* previous = new Node<T>;
        Node<T>* current = new Node<T>;
        Node<T>* temp = new Node<T>;
        current = head;
        for(int i  = 1; i < pos; i++) {
            previous = current;
            current = current->next;
        }
        temp->data = theData;
        previous->next = temp;
        temp->next = current;
    }

    void delete_first() {
        Node<T>* temp = new Node<T>;
        temp = head;
        head = head->next;
        delete temp;
    }

    void delete_last() {
        Node<T>* previous = new Node<T>;
        Node<T>* current = new Node<T>;
        current = head;
        while(current->next != nullptr) {
            previous = current;
            current = current->next;
        }
        tail = previous;
        previous->next = nullptr;
        delete current;
    }

    void delete_position(int pos) {
        Node<T>* previous = new Node<T>;
        Node<T>* current = new Node<T>;
        current = head;
        for(int i = 1; i < pos; i++) {
            previous = current;
            current = current->next;
        }
        previous->next = current->next;
    }

    bool search(Node<T>* head, int x) {
        struct Node<T>* current = head;  
        while (current != NULL) {
            if (current->data == x)
                return true;
            current = current->next;
        }
        return false;
    }
};

#endif /* LinkedList_hpp */

这是main.cpp:

#include <iostream>
#include "LinkedList.hpp"


int main(int argc, const char * argv[]) {

    SingleLinkedList<int> obj;
    obj.createNode(2);
    obj.createNode(4);
    obj.createNode(6);
    obj.createNode(8);
    obj.createNode(10);
    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"---------------Displaying All nodes---------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-----------------Inserting At End-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.createNode(55);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Inserting At Start----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insert_start(50);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-------------Inserting At Particular--------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insert_position(5,60);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Deleting At Start-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.delete_first();
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-----------------Deleing At End-------------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.delete_last();
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"--------------Deleting At Particular--------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.delete_position(4);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    system("pause");

    Node<int>* head = NULL;
    obj.search(head, 8) ? printf("Yes") : printf("No");


    return 0;
}

你可以看到我搜索了值8,它应该打印是,因为8仍然在链表中。

2 个答案:

答案 0 :(得分:2)

这是您的search功能:

bool search(Node<T>* head, int x) {
    struct Node<T>* current = head;  
    while (current != NULL) {
        if (current->data == x)
            return true;
        current = current->next;
    }
    return false;
}

它使用传入的参数head作为第一个参数,而不是SingleLinkedList<T>::head成员变量。

由于你称之为传递空指针作为第一个参数,你将找不到任何东西。

简单修复:从search函数中删除第一个参数:

bool search(T x) { ... }

如您所见,我还将您搜索的值的参数更改为模板类型。

答案 1 :(得分:1)

这一行:

Node<int>* head = NULL;

表示您将NULL传递给搜索方法。

您的搜索方法根本不应该使用头部参数 - head已经是您班级的成员。

另请注意:您有很多内存泄漏:

Node<T>* temp = new Node<T>;
temp = head;

您不需要为刚刚用新值覆盖的指针分配内存,只需使用

Node<T>* temp = head;