Linked list questions

How would you copy a linked list or arrange a linked list to a certain value inside the nodes?
By arrange, do you mean sort?
yeah or just switch the posistion from one node to another
closed account (D80DSL3A)
Just swap the prev and next pointer assignments for the 2 nodes.
fun2code wrote:
Just swap the prev and next pointer assignments for the 2 nodes.
And of course the nodes pointing to them ;)
closed account (D80DSL3A)
@Mathhead200 Of course...
I felt that the carelessly phrased question deserved a similarly phrased answer.
Hey! Memory leaks are not funny fun2code... Okay, maybe a little... :)
Why would you do that? Just swap the data fields in the two nodes, assuming that's how your nodes are structured.
closed account (D80DSL3A)
I would do that because the data fields may be large, complex structures. Changing a few pointer assignments and leaving the data in place may often be more efficient.
This especially applies when sorting a list, which typically involves making many swaps.

I think this is a principal advantage that a linked list offers - the ability to change the order of the nodes while leaving the data in place in memory.
Topic archived. No new replies allowed.