c - change of one pointer affect other pointer -
how possible komv->next become null on code before end of while loop? notice become null after line town->previous->next=town->next cant understand why happened. programme terminated segmentation fault.
komv=list->first; while ((komv->next)!=null) { if(town->num>=komv->next->num) { town->previous->next=town->next; if(town->next!=null) town->next->previous=town->previous; town->next=komv->next; town->previous=komv; komv->next->previous=town; // gdb komv->next=null komv->next=town; break; } komv=komv->next; }
if komv
node preceding town
in list, town->previous == komv
. if town
lastly node town->next == null
. when true @ same time, this:
town->previous->next=town->next;
reduces to:
komv->next = null;
in case, looks there no need modify list, prepare problem modifying status to
if ((town != komv->next) && (town->num >= komv->next->num)) {
there might improve solution, too, depending on assumptions , behavior want. example, if acceptable insert town
after nodes having same num
instead of before, utilize condition:
if (town->num > komv->next->num) {
c pointers struct
No comments:
Post a Comment