Thursday, August 19, 2010

How to delete a node from the middle of a linked list & how to....?

....insert the deleted node at the beginning of another linked list which stores all the free memory spaces.kindly provide an algorithm for the above mentioned problem .

How to delete a node from the middle of a linked list %26amp; how to....?
There are several ways to do this, here's a simple one:


1. Traverse until the node (let this be called node N) that you want to delete - keeping track of the current node and the previous node (i.e. have 2 pointers to keep track)


2. Make a new local pointer that points to node N.


3. Set the previous node's (through the previous pointer) next element to the address of node N's next address. i.e. something like: prevPointer-%26gt;next = curPointer-%26gt;next; (This effectively "deletes" the node off the list)


4. Now with the saved local pointer in Step 2, prepend it to the other linked list you mentioned (no need to free it because we are simply re-using the same node through pointer manipulation) - I'm assuming you know how to prepend but in case you don't. You just set node N's next address to the current head of the list, and set the new head of the list to be equal to node N's address.
Reply:Visit My blog's http://codesbyshariq.blogspot.com Blog Archive section for the program of Linked Lists and other Data Structures
Reply:u can refer books of diff. like balagurusamy,kanitkar,schaum,ritchie,etc...


its hard to explain in this way


No comments:

Post a Comment