Thursday, August 19, 2010

C Programming: How do I delete the last node of a linked list?

Also, how do I print a linked list in reverse order?

C Programming: How do I delete the last node of a linked list?
to delete the last, always keep a pointer to the previous element as you go along until you hit the end, then set pev-%26gt;next = NULL;





the easiest way to print a linked list in reverse is with a recursive function (this aint code, just a clue):





printreverse(ptr)


{


if(ptr-%26gt;next == NULL)


printf("%s\n",ptr-%26gt;value);


else {


printreverse(ptr-%26gt;next);


printf("%s\n",ptr-%26gt;value);


}


}
Reply:in reverse order just use the pointer for the last object. It depends on how you have it set up... If you have it set up in an array of pointers you can just say myArrayPointer[-1].item. This is hard to answer without seeing your code. Mabey you could post your code on here?


No comments:

Post a Comment