C - add element to sorted-linked-list (alphabetical sorting)

北城余情 提交于 2020-06-17 14:20:08

问题


I have to do some kind of work here. My task is to write words from file to a struct in alphabetical order. I thought "hmmmm.. that's easy", but not - it isn't. Or just I f*cked it up :( Can you guys tell me how should I alphabetically add words to my list using these 3 functions? Or maybe it is other way to do this?

I share most of my code.


struct Word
{
  char* word;
  struct Word* pNext;
};

typedef struct Word Word;

bool IsLegitWord(char* word){
  int word_length = strlen(word);
  int i = 0;
  for(i = 0; i < word_length; i++){
    if(isalpha(word[i]) == 0){
      return false;
    }
  }
  return true;
}

void AddFront(Word** pH, char* word)
{
    /* 1. allocate node */
    Word* new_node = (Word*) malloc(sizeof(Word));

    /* 2. put in the data  */
    new_node->word = (char *)malloc((strlen(word)+1)*sizeof(char));
    strcpy(new_node->word, word);

    /* 3. Make next of new node as head */
    new_node->pNext = (*pH);

    /* 4. move the head to point to the new node */
    (*pH)    = new_node;
}

void InsertAfter(Word* previous, char* word)
{
    /*1. check if the given prev_node is NULL */
    if (previous == NULL)
    {
      printf("the given previous node cannot be NULL");
      return;
    }

    /* 2. allocate new node */
    Word* new_node =(Word *) malloc(sizeof(Word));

    /* 3. put in the data  */
    new_node->word = (char *)malloc((strlen(word)+1)*sizeof(char));
    strcpy(new_node->word, word);

    /* 4. Make next of new node as next of prev_node */
    new_node->pNext = previous->pNext;

    /* 5. move the next of prev_node as new_node */
    previous->pNext = new_node;
}

/* Given a reference (pointer to pointer) to the head
   of a list and an int, appends a new node at the end  */
void AddAtEnd(Word** pH, char* word)
{
    /* 1. allocate node */
    Word* new_node = (Word*) malloc(sizeof(Word));

    Word* last = *pH;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->word = (char *)malloc((strlen(word)+1)*sizeof(char));
    strcpy(new_node->word, word);

    /* 3. This new node is going to be the last node, so make next of
          it as NULL*/
    new_node->pNext = NULL;

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*pH == NULL)
    {
       *pH = new_node;
       return;
    }

    /* 5. Else traverse till the last node */
    while (last->pNext != NULL)
        last = last->pNext;

    /* 6. Change the next of last node */
    last->pNext = new_node;
    return;
}


int NumberOfWords(Word* pH){
  int count = 0;
  Word* temp = pH;
  while (temp != NULL)
  {
    count++;
    temp = temp->pNext;
  }
  return count;
}

void InsertWordsToStruct(Word** pH, FILE* filename){
  char single_line[16384]; /* longest possible line in my code */

  int number_of_words = 0;
  int counter = 0;

  Word* temp = *pH; /* idk is it necessary */

  while(fgets(single_line, 16384, filename))
  {
    char* single_word = strtok(single_line, " \t\n\0"); /* cut one word from line */
    while(single_word != NULL)
    {
      counter = 0;
      temp = *pH;

      if(IsLegitWord(single_word) == true) /* function return true if word is really word */
      {
        number_of_words = NumberOfWords(*pH); /* function gets number of words */
        if(number_of_words == 0)
        {
          AddAtEnd(pH, single_word);
          single_word = strtok(NULL, " \t\n\0");
          continue;
        }
        while((strcmp(single_word, temp->word) < 0) && counter < number_of_words)
        {
          counter++;
          temp = temp->pNext;
        }
        if((counter == 0) && strcmp(single_word, temp->word) > 0)
        {
          AddFront(pH, single_word);
          continue;
        }
        if(temp == NULL)
        {
          AddAtEnd(pH, single_word);
          single_word = strtok(NULL, " \t\n\0");
          continue;
        }
        if(strcmp(single_word, temp->word) == 0)
        {
          InsertAfter(temp->pNext, single_word);
          continue;
        }
      }
      single_word = strtok(NULL, " \t\n\0");
    }
  }
}

I will appreciate any help!

来源:https://stackoverflow.com/questions/62150035/c-add-element-to-sorted-linked-list-alphabetical-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!