bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current) { while(temp->next!=current) { temp=temp->next; current=temp; } temp=head; return current; }
He vinculado la lista y el puntero ‘cabeza’ mantiene el primer nodo, el ‘actual’ puntero mantiene el último nodo, quiero llevar ‘stream’ a la cabeza uno por uno, por lo que escribo esta función pero da un error de segmentación cuando depuro el progtwig
temp->next!=current
nunca será verdadero a menos que temp==temp->next
.
Prueba esto:
bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current) { while(temp->next!=current) { temp=temp->next; } current=temp; /* get this out of the loop */ temp=head; return current; }
O más simplificado esto:
bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current) { (void)head; /* head isn't used, so put this to avoid warning */ while(temp->next!=current) { temp=temp->next; } /* current and temp will be lost, so assigning to them here is useless */ return temp; }
Para hacerlo más seguro, asegúrese de que la temp
no sea NULL
para evitar el error de tiempo de ejecución en caso de que la current
no sea válida.
bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current) { (void)head; /* head isn't used, so put this to avoid warning */ while(temp != NULL && temp->next!=current) { temp=temp->next; } /* temp will be the previous node of current or NULL */ return temp; }
Tal vez quieras esto:
bn_ptr drive_temp(bn_ptr head,bn_ptr current) /* remove temp from the arguments */ { bn_ptr temp = head; while(temp != NULL && temp->next!=current) { temp=temp->next; } /* temp will be the previous node of current or NULL */ return temp; }