La causa sta in questa riga

if(temp->next->numero%z==0)

in quanto temp->next, ad un certo punto, è NULL