Q:

search and delete a node from linked list

I am writing a simple dictionary program using linked list. I want to search a word in the dictionary and delete it. I've wrote the code, but I think It's more time consuming as I'm running the loop two times, 1st to search the node and note down the position and 2nd to delete it.
struct node{
    char word[20];
    char meaning[5][100];
    struct node *next;
};

void del(struct node *head, char *word)
{
  int found = 0, position = 0, i;
  struct node *temp = head;
  while(temp != NULL)
  {
    if(strcmp(temp->word, word) == 0)
    {
        found = 1;
        break;
    }
    temp = temp->next;
    position++;
  }
  if(found == 1)
  {
      temp = head;
      if(position == 0)
      {
          head = temp->next;
          free(temp);
      }
      for(i = 0; i < position-1; i++)
          temp = temp->next;
      struct node *temp2 = temp->next;
      temp->next = temp2->next;
      free(temp2);
      printf("Word deleted..\n");
  }
  else printf("Word not found!\n");
}

is there any alternate way to optimize the program?

A:

You just have to merge the two cycles together like this, here's a code example.
struct node{
    char word[20];
    char meaning[5][100];
    struct node *next;
};

struct node *del(struct node *head, char *word)
{int found = 0, position = 0, i;
  struct node *temp = head;
  struct node *prev = NULL;
  /*You should avoid breaks because they decrease legibility*/
  while(temp != NULL)
  {

    if(strcmp(temp->word, word) == 0)
    {
        if(prev == NULL){ /*If the node is the head*/
            head = head->next;
            free(temp);
            return head;
        }else{
            prev->next = temp->next;
            free(temp);
            return head;
        }
    }
    prev = temp;
    temp = temp->next;
  }

}

