Linked list find function in C

Viewed 37

I've been working on a linked list which was implemented by the name vector_string. I'm trying to insert words into it, if the word is already existed in the linked list, skip it.

void vector_string_insert(vector_string *vs, char *key) {
  vs_entry_t *newNode = (vs_entry_t*)malloc(sizeof(vs_entry_t));
  newNode->value = key;
  newNode->next = NULL;

  vs_entry_t *current = vs->head;

  if(vs->head == NULL)
  {
    vs->head = newNode;
  }
  else{
    while(current->next != NULL)
      current = current->next;

    current->next = newNode;
  }
}

bool vector_string_find(vector_string *vs, char *key) 
{
  vs_entry_t *current = vs->head;

  while(current != NULL)
  {
    if (current->value == key)
      return true;

    current = current->next;
  }
  return false;   
} 

And here's my main:

  if (vector_string_find(header, "Hello") == false);
    vector_string_insert(header,"Hello");
  if (vector_string_find(header, "Hello") == false);
    vector_string_insert(header,"Hello");
  if (vector_string_find(header, "Hola") == false);
    vector_string_insert(header,"Hola");
  

Although the vector_string_find() returned "true" for the second "Hello", it still got inserted in the linked list:

1. Hello
2. Hello
3. Hola
0 Answers
Related