Быстрый поиск связанного списка не компилируется, требуется возврат - PullRequest
0 голосов
/ 19 сентября 2019

Ниже приведен следующий код, где у меня есть несколько манипуляций со связанным списком.Единственная функция, на которой я фокусируюсь - это searchFast().Перед тем, как скомпилировать код, я получаю предупреждение о том, что «нет возврата в функции, возвращающей non-void [-Wreturn-type].» *

Я думал о том, что я добавляю return 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;
}

1 Ответ

0 голосов
/ 19 сентября 2019

В итоге я добавил return NULL и return p, и код успешно скомпилирован.

#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;
    }
...