-2

I wrote a linked list and I don't understand how to realize a search element in the linked list.

I understand the first part of the code, but not the part that starts with if(found == true).

Thank you in advance if anyone decides to respond.


struct test_struct
{
    int val;
    struct test_struct *next;
};

struct test_struct *head = NULL;
struct test_struct *curr = NULL;

struct test_struct* create_list(int val)
{
    ...
}

struct test_struct *add_to_list(int val, bool add_to_end)
{
    ...
}

struct test_struct* search_in_list(int val, struct test_struct **prev)  
{
    struct test_struct *ptr = head;  
    struct test_struct *tmp = NULL;  
    bool found = false;

    while (ptr != NULL)
    {
        if (ptr->val == val)
        {
            found = true;
            break;
        }
        else
        {
            tmp = ptr; // here tmp play role of previous pointer  
            ptr = ptr->next;
        }
    }

    if (true == found)
    {
        if (prev)
            *prev = tmp;
        return ptr;
    }
    else
    {
        return NULL;
    }
}
Reyedy
  • 918
  • 2
  • 13
  • 36

1 Answers1

0

I assume your test_struct is as following:

struct test_struct
{  
    int val;  
    struct test_struct *next;  
};

I also assume that your head is declared somewhere in your code as following:

struct test_struct *head;

and that your linked list is filled somewhere.

In your search function, you need to use the val attribute that you set, which is the value you are looking for. In the code you posted, it's not used anywhere. Also, there are some typos, using temp instead of tmp.

struct test_struct* search_in_list(int val, struct test_struct **prev) {
    struct test_struct *ptr = head;  
    struct test_struct *tmp = NULL;  
    bool found = false;

    while (ptr != NULL) {
        if (ptr->val == val) { /* val and not data */
            found = true;
            break;
        } else {
            ptr = tmp; /* tmp and not temp */
            ptr = ptr->next;
        }
    }
    if (found == true) {
        if (prev)
            *prev = tmp; /* tmp and not temp */
        return ptr;
    }
    else {
        return NULL;
    }
}

In this function, the prev attribute is set to tmp at the end of the research. It means that it saves the previous element of the linked list compared to the one that was researched. Keep this only if you have a use for it!

Reyedy
  • 918
  • 2
  • 13
  • 36
  • thanks. but I don't understand why we write prev in the round brackets, and why we don't write curly brackets after if in the case? ``` if (found == true) { if (prev) *prev = tmp; /* tmp and not temp */ return ptr;}``` – gleb_arbuzov Apr 01 '20 at 10:11
  • curly brackets are optional when there is only one instruction in the "if" block. More information here: https://stackoverflow.com/questions/14901919/when-can-i-omit-curly-braces-in-c – Reyedy Apr 01 '20 at 10:14
  • but what means prev in curly brackets? – gleb_arbuzov Apr 01 '20 at 10:19
  • I think it's to make sure that the "prev" param is set when the function is called. This way, the "prev" param is optional, and it will be set to the tmp value only if it has been set in the function call. – Reyedy Apr 01 '20 at 10:21
  • thank so much, but i have another question,. so, for what it is used here pointer to pointer : struct test_struct **prev? – gleb_arbuzov Apr 01 '20 at 10:28
  • I invite you to read this thread, that should help you understand: https://stackoverflow.com/questions/18698317/pointers-as-function-arguments-in-c Understanding pointers is a complex subject in C and cannot be explained with only a comment – Reyedy Apr 01 '20 at 10:41
  • Glad I could help. Feel free to mark my answer as accepted if that answered your question properly. Have a good day :) – Reyedy Apr 01 '20 at 10:45
  • I changed the code. I looked at the output. And I came to the conclusion that this is the part when if(found== true... and so on is very important in finding the item! But why? Maybe these lines of code are needed to check? What do you think? – gleb_arbuzov Apr 01 '20 at 15:48