gpt4 book ai didi

c++ - 在单个链表中搜索特定值

转载 作者:行者123 更新时间:2023-11-30 05:01:03 25 4
gpt4 key购买 nike

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

这是我的代码:

#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,它应该打印 yes,因为 8 仍在链表中。

最佳答案

这是你的 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) { ... }

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

关于c++ - 在单个链表中搜索特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com