Friday 15 August 2014

c - Delete function in circular linked list changing next and previous to null when only one node left -



c - Delete function in circular linked list changing next and previous to null when only one node left -

in delete function circular doubly linked list when come in function 2 nodes , delete 1 node changing next node , previous node null. happens when come in function 2 nodes in linked list. using breakpoints in eclipse , seems working until end function , homecoming temp. after next , previous ptrlocal set null. not sure why.

here function call

struct tcb_t del = delete_from_list(&runq);

here function

struct tcb_t delete_from_list(struct tcb_t **ptrlocal) { struct tcb_t temp; if(*ptrlocal) { temp = **ptrlocal; temp.next = null; temp.previous =null; if(*ptrlocal == (*ptrlocal)->next->next) { *ptrlocal = (*ptrlocal)->next; (*ptrlocal)->next = *ptrlocal; (*ptrlocal)->previous = *ptrlocal; } else if(*ptrlocal != (*ptrlocal)->next) { (*ptrlocal)->previous->next = (*ptrlocal)->next; (*ptrlocal)->next->previous = (*ptrlocal)->previous; *ptrlocal = (*ptrlocal)->next; } else { (*ptrlocal)->previous = null; (*ptrlocal)->next = null; *ptrlocal =null; } count--; } homecoming temp; }

after homecoming temp ptrlocal->next , prelocal->previous both set null.

your error lastly else. applied when there single node in list.

in circular linked list, next , previuos never shouldn't null

therefor if there 1 item, next , previous should point itself.

now should check this:

if ((*ptrlocal)->next = (*ptrlocal)){ //delete lastly item in list, should null pointer free(*ptrlocal); *ptrlocal=null; } else { (*ptrlocal)->previous->next = temp->next; (*ptrlocal)->next->previous = temp->previous; free(*ptrlocal); }

i don't see reason check 2 items:

for illustration a<->b<->a

and delete b:

if go else: a<->a still circular list.

c linked-list nodes

No comments:

Post a Comment