Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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++_Data Structures_Linked List_Nodes - Fatal编程技术网

C++ 快速搜索链表未编译,需要返回语句

C++ 快速搜索链表未编译,需要返回语句,c++,data-structures,linked-list,nodes,C++,Data Structures,Linked List,Nodes,下面给出了以下代码,其中有几个链表操作。我关注的一个功能是searchFast()。在编译代码之前,我收到一条警告:“函数中没有返回语句返回non void[-Wreturn type]” 我的想法是添加一个返回NULL。但这似乎并不正确 请参阅以下代码: #include <iostream> #include <stdio.h> using namespace std; struct Node{ //if you want to convert to class,

下面给出了以下代码,其中有几个链表操作。我关注的一个功能是
searchFast()
。在编译代码之前,我收到一条警告:“函数中没有返回语句返回non void[-Wreturn type]”

我的想法是添加一个
返回NULL
。但这似乎并不正确

请参阅以下代码:

#include <iostream>
#include <stdio.h>
using namespace std;

struct Node{ //if you want to convert to class, you need to assign access specifiers to public since they're private on default

    int data;
    struct Node *next;

}*first = NULL;

void create(int A[], int n)
{

    struct Node *t; //create a temporary pointer t
    struct Node *last; //pointer, points to the last node - helps me add a new node at the END of a linked list
    //as of now, the linked list is empty, so we must create the first node!

    first = new Node;//create new node on the heap, and first will be pointing on that new node
    first -> data = A[0]; // Assign first the first element on the array
    first -> next = NULL;//Should point to a null value as it is the only element on the list to start/currently
    last = first; //last points on first node

    for (int i = 1; i <n; i++)// i starts at element 1 since first has been assigned the 1st element in the array
    {
        t = new Node; //create a new node
        t->data = A[i]; //fill up the data of t from A[i] which will be iterated through and assigned to data
        t->next = NULL; // the next node should be pointing to NULL, as there is nothing at the moment when the iteration begins that it is initially pointing to

        last -> next = t;
        last = t;
    }
}

Node * searchFast( struct Node * p, int key){
    Node * q = NULL;
    while (p != NULL){
        if(key == p->data){
            q->next = p->next;
            p->next = first;
            first = p;
            return p;

        }
        q = p;
        p = p->next;
    }
    return NULL;
}

void display (struct Node *p)
{
    while (p != 0 )
    {
        cout<<p->data<<" ";
        cout<<p->next<<" ";
        p = p->next;
    }

}

int main() {

    int A [] = {1,2,3,18,5, 6, 7};

    create (A,7);
    display(first);


    cout<<"If following value 18 can be found in the linked list, its address is the following: "<<searchFast(first, 18);

    return 0;
}
#包括
#包括
使用名称空间std;
结构节点{//若要转换为类,则需要将访问说明符分配给public,因为它们在默认情况下是私有的
int数据;
结构节点*下一步;
}*第一个=空;
无效创建(int A[],int n)
{
struct Node*t;//创建一个临时指针t
struct Node*last;//指针,指向最后一个节点-帮助我在链表末尾添加新节点
//现在,链表是空的,所以我们必须创建第一个节点!
first=new Node;//在堆上创建新节点,first将指向该新节点
first->data=A[0];//首先分配数组中的第一个元素
first->next=NULL;//应该指向NULL值,因为它是列表中唯一要启动/当前启动的元素
last=first;//第一个节点上的最后一个点
for(int i=1;i data=A[i];//从A[i]中填充t的数据,该数据将被迭代并分配给数据
t->next=NULL;//下一个节点应该指向NULL,因为在迭代开始时没有它最初指向的节点
last->next=t;
last=t;
}
}
Node*searchFast(结构节点*p,int键){
Node*q=NULL;
while(p!=NULL){
如果(键==p->数据){
q->next=p->next;
p->next=第一;
第一个=p;
返回p;
}
q=p;
p=p->next;
}
返回NULL;
}
无效显示(结构节点*p)
{
while(p!=0)
{

cout我最后添加了returnnull和returnp,代码编译成功

#include <iostream>
    #include <stdio.h>
    using namespace std;

    struct Node{ //if you want to convert to class, you need to assign access specifiers to public since they're private on default

        int data;
        struct Node *next;

    }*first = NULL;

    void create(int A[], int n)
    {

        struct Node *t; //create a temporary pointer t
        struct Node *last; //pointer, points to the last node - helps me add a new node at the END of a linked list
        //as of now, the linked list is empty, so we must create the first node!

        first = new Node;//create new node on the heap, and first will be pointing on that new node
        first -> data = A[0]; // Assign first the first element on the array
        first -> next = NULL;//Should point to a null value as it is the only element on the list to start/currently
        last = first; //last points on first node

        for (int i = 1; i <n; i++)// i starts at element 1 since first has been assigned the 1st element in the array
        {
            t = new Node; //create a new node
            t->data = A[i]; //fill up the data of t from A[i] which will be iterated through and assigned to data
            t->next = NULL; // the next node should be pointing to NULL, as there is nothing at the moment when the iteration begins that it is initially pointing to

            last -> next = t;
            last = t;
        }
    }

    Node * searchFast( struct Node * p, int key){
        Node * q = NULL;
        while (p != NULL){
            if(key == p->data){
                q->next = p->next;
                p->next = first;
                first = p;
                return p;

            }
            q = p;
            p = p->next;
        }
        return NULL;
    }

    void display (struct Node *p)
    {
        while (p != 0 )
        {
            cout<<p->data<<" ";
            cout<<p->next<<" ";
            p = p->next;
        }

    }

    int main() {

        int A [] = {1,2,3,18,5, 6, 7};

        create (A,7);
        display(first);


        cout<<"If following value 18 can be found in the linked list, its address is the following: "<<searchFast(first, 18);

        return 0;
    }
#包括
#包括
使用名称空间std;
结构节点{//若要转换为类,则需要将访问说明符分配给public,因为它们在默认情况下是私有的
int数据;
结构节点*下一步;
}*第一个=空;
无效创建(int A[],int n)
{
struct Node*t;//创建一个临时指针t
struct Node*last;//指针,指向最后一个节点-帮助我在链表末尾添加新节点
//现在,链表是空的,所以我们必须创建第一个节点!
first=new Node;//在堆上创建新节点,first将指向该新节点
first->data=A[0];//首先分配数组中的第一个元素
first->next=NULL;//应该指向NULL值,因为它是列表中唯一要启动/当前启动的元素
last=first;//第一个节点上的最后一个点
for(int i=1;i data=A[i];//从A[i]中填充t的数据,该数据将被迭代并分配给数据
t->next=NULL;//下一个节点应该指向NULL,因为在迭代开始时没有它最初指向的节点
last->next=t;
last=t;
}
}
Node*searchFast(结构节点*p,int键){
Node*q=NULL;
while(p!=NULL){
如果(键==p->数据){
q->next=p->next;
p->next=第一;
第一个=p;
返回p;
}
q=p;
p=p->next;
}
返回NULL;
}
无效显示(结构节点*p)
{
while(p!=0)
{

可能函数searchFast()中没有return语句。我的想法是添加一个return NULL。但这似乎不对。如果未找到,则需要返回nullptr,如果找到,则需要返回节点指针。if(key==p->data)之后的块{
看起来很奇怪。我不希望在搜索函数中出现这种行为。@drscherjm OP需要一个
返回值
才能成功搜索…所以如果(key==p->data)返回p,则应该是
;否则{
q->next=p->next;p->next=first;first=p;
仍然是错误的。您只需返回
p
其他内容。不更改节点。不重置头指针。