Обновить ячейку в списке структур и вернуть список - PullRequest
0 голосов
/ 03 января 2019

Я ищу способ обновить ячейку в списке структуры. У меня есть список структуры:

struct _list {
  int startNode,
      targetNode,
      substitute;
  unsigned char letter;
  struct _list *next;
}

И функция, которая позволяет мне обновлять ячейку.

struct _list *insertSubstitute(struct _list *list, int startNode,
    unsigned char letter, int substitute) {
  while (list != NULL) {
      if (list -> letter == letter && list -> startNode == startNode) {
        list -> substitute = substitute;
        return list;
      }
      list = list -> next;
  }
  return NULL;
}

Моя проблема в том, что эта функция не возвращает весь список. Как я могу приступить к этому?

Ответы [ 3 ]

0 голосов
/ 03 января 2019

вам нужно просто использовать временный указатель на ваш список для проверки элементов, так как вы используете список сам по себе, что вызывает у вас проблему

struct _list *insertSubstitute(struct _list *list, int startNode,
unsigned char letter, int substitute) {
 struct _list *temp=list;
 while (temp!= NULL) {
   if (temp-> letter == letter && temp-> startNode == startNode) {
    temp -> substitute = substitute;
    break;
  }
  temp= temp-> next;
  }
   return list;
}
0 голосов
/ 03 января 2019

Попробуйте:

    void insertSubstitute(struct _list** list
                         ,int startNode
                         ,unsigned char letter
                         ,int substitute) {
       if ( list == NULL ) {
    //Do nothing list is invalid
          return;
       }
       while (*list != NULL) {
          if ((*list) -> letter == letter && (*list) -> startNode == startNode) {
            (*list) -> substitute = substitute;
            return;
          }
          (*list) = (*list) -> next;
       }
    }

Когда вы вызываете эту версию функции, вам нужно передать адрес вашего списка.

Или другую версию оригинала:

    struct _list*insertSubstitute(struct _list* list
                                 ,int startNode
                                 ,unsigned char letter
                                 ,int substitute) {
       if ( list == NULL ) {
    //Do nothing list is invalid
          return;
       }
       struct _list* rc = list;
       while (rc != NULL) {
          if (rc -> letter == letter && rc -> startNode == startNode) {
            rc -> substitute = substitute;
            return rc;
          }
          rc = rc -> next;
       }
       return NULL;
    }
0 голосов
/ 03 января 2019

Добавить новую переменную whole_list.

Может работать следующее code:

struct _list *insertSubstitute(struct _list *list,
                               int startNode,
                               unsigned char letter,
                               int substitute) {

  struct _list* whole_list = list;

  while (list != NULL) {
      if (list -> letter == letter && list -> startNode == startNode) {
        list -> substitute = substitute;
        break;
      }
      list = list -> next;
  }
  return whole_list;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...