Question

I am developing a C program and I want to use this function to check if an element of type Client with a given number is already inserted. It moves along the list and returns 1 as soon as the number is found, only when I try to compile, it tells me the "prev" variable is not used.

int list_search_number(LinkedListNode head, Client value){                      
    LinkedListNode prev, curr;

    prev = NULL;
    curr = head;

    while (curr != NULL) { 
          if(curr->value.number!=value.number){ 
            prev = curr;
            curr = curr->next;}                   
          else if(curr->value.number==value.number)
            return 1;                                
    }      
return 0;                           
}

The linked list and Client structures are defined correctly.

Was it helpful?

Solution

You do not need to store prev pointer since there is no need for it. Remove prev variable and go on.

Change the algorithm to return 1 if the number is found and return 0 if it is not found. prev pointer is useless.

Here is the code.

int list_search_number(LinkedListNode head, Client value){                      
    LinkedListNode curr;


    curr = head;

    while (curr != NULL) { 
          if(curr->value.number==value.number){ 
            return 1;
            }                   
          else {
                curr = curr->next;      
            }                      
    }      
return 0;                           
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top