Usuwanie z listy

0

Próbuję usunąć wszystkie parzyste elementy z listy. Jednak algorytm nie działa poprawnie :

struct node *delDiv2(struct node *lista) {
  while((lista->value%2) == 0 ) {
    lista = lista->next;
  }
  struct node *temp = lista;
  struct node *prev;
  while(lista) {
    if((lista->value %2) == 0) {
      prev->next = lista->next;
    }
    prev = lista;
    lista = lista->next;
  }

  return temp;
};

 
0

Hej,

sprobuj tego:

void List::delDiv2()
{
    Node *tmp, *prev, *current;
    current = head;
    
    if (head == NULL) {
        return;
    }

    if (head != NULL && (head->value % 2) == 0) {
        tmp = head;
        head = head->next;
        delete tmp;
    }

    current = head;
    while (current) {

        if (current->value % 2 == 0) {
            tmp = current;
            current = current->next;
            prev->next = current;
            delete tmp;
        } else {
            prev = current;
            current = current->next;
        }
    }
}

Pozdrawiam,

0
struct node *delDiv2(struct node *lista)
  {
   struct node **curr=&lista,*temp;
   while(*curr)
     {
      temp=*curr;
      if(temp->value%2)  curr=&temp->next;
      else
        {
         *curr=temp->next;
         free(temp);
        }
     } 
   return lista;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1