Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在单链表中搜索特定值_C++_Xcode_Linked List - Fatal编程技术网

C++ 在单链表中搜索特定值

C++ 在单链表中搜索特定值,c++,xcode,linked-list,C++,Xcode,Linked List,我正在尝试实现一个通用的单链表。到目前为止,我做的一切都是正确的,但我不能让我的搜索功能正常工作。它应该在输出中打印“是”,但什么也没有发生 这是我的密码: #ifndef LinkedList_hpp #define LinkedList_hpp #include <iostream> template<class T> struct Node { T data; Node<T>* next; }; template<class

我正在尝试实现一个通用的单链表。到目前为止,我做的一切都是正确的,但我不能让我的搜索功能正常工作。它应该在输出中打印“是”,但什么也没有发生

这是我的密码:

#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 */
\ifndef LinkedList\u水电站
#定义LinkedList_hpp
#包括
模板
结构节点{
T数据;
节点*下一步;
};
模板
类SingleLinkedList{
私人:
节点*头;
节点*尾部;
公众:
SingleLinkedList(){
水头=零PTR;
tail=nullptr;
}
void createNode(常量T和数据){
Node*temp=新节点;
温度->数据=数据;
temp->next=nullptr;
if(head==nullptr){
压头=温度;
尾=温度;
温度=零PTR;
}
否则{
尾部->下一步=温度;
尾=温度;
}
}
无效显示(){
Node*temp=新节点;
温度=水头;
while(temp!=nullptr){
std::cout数据下一步;
}
}
无效插入\u开始(常数和数据){
Node*temp=新节点;
温度->数据=数据;
温度->下一步=头部;
压头=温度;
}
无效插入位置(内部位置、常量和数据){
节点*previous=新节点;
节点*当前=新节点;
Node*temp=新节点;
电流=水头;
对于(int i=1;i下一步;
}
温度->数据=数据;
上一个->下一个=温度;
温度->下一步=当前;
}
void delete_first(){
Node*temp=新节点;
温度=水头;
头部=头部->下一步;
删除临时文件;
}
void delete_last(){
节点*previous=新节点;
节点*当前=新节点;
电流=水头;
while(当前->下一步!=nullptr){
先前=当前;
当前=当前->下一步;
}
尾=前一个;
上一个->下一个=nullptr;
删除当前文件;
}
无效删除位置(内部位置){
节点*previous=新节点;
节点*当前=新节点;
电流=水头;
对于(int i=1;i下一步;
}
上一个->下一个=当前->下一个;
}
布尔搜索(节点*头部,整数x){
结构节点*当前=头部;
while(当前!=NULL){
如果(当前->数据==x)
返回true;
当前=当前->下一步;
}
返回false;
}
};
#endif/*LinkedList_水电站*/
以下是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;
}
#包括
#包括“LinkedList.hpp”
int main(int argc,const char*argv[]{
单链接列表obj;
对象创建节点(2);
对象创建节点(4);
对象创建节点(6);
对象创建节点(8);
对象创建节点(10);

std::cout这是您的
搜索功能:

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;
}
bool search(T x) { ... }
如您所见,我还将您搜索的值的参数更改为模板类型。

此行:

Node<int>* head = NULL;
您不需要为要用新值覆盖的指针分配内存,只需使用

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

Node*head=NULL;
…这是您正在搜索的列表吗?这是一个空列表。感谢我修复了您的建议。除了这些之外,还有其他内存泄漏吗?通常,您希望确保您的
删除
配对。对于链接列表,您可能只想在“删除类型”方法中的“创建/插入类型”方法和“删除”。也就是说,您当前不
删除
位置中的项目,也不
删除析构函数中的所有项目(因为您没有一个项目)另外两件事需要注意的是,你没有在任何地方使用
tail
,而且你也没有在任何
insert.*
delete.*
方法中保持它的最新状态(除了
delete.\u last
),最好不要使用它由于当前您的类不可安全复制。谢谢您的观点。我现在创建了一个析构函数,但是我在哪里调用delete?您可以引用一个我将调用delete的函数吗?当您的类的对象被销毁时,将自动调用析构函数(例如,
obj
在主管道末端超出范围时被销毁)。此时,您应该调用
delete
,删除使用
new
创建的任何内容,在这种情况下,这意味着删除列表中的所有元素。请注意,这不会导致当前程序出现问题,因为您仍将退出该程序,并且所有内存将释放回操作系统。但是,如果您打算使用clas在运行时间较长的程序中,您应该始终清理内存。
Node<T>* temp = head;