Linked list questions

Jun 16, 2011 at 1:25am
How would you copy a linked list or arrange a linked list to a certain value inside the nodes?
Jun 16, 2011 at 1:58am
By arrange, do you mean sort?
Jun 16, 2011 at 3:44am
yeah or just switch the posistion from one node to another
Jun 16, 2011 at 5:15am
closed account (D80DSL3A)
Just swap the prev and next pointer assignments for the 2 nodes.
Jun 16, 2011 at 8:19am
fun2code wrote:
Just swap the prev and next pointer assignments for the 2 nodes.
And of course the nodes pointing to them ;)
Jun 16, 2011 at 9:43am
closed account (D80DSL3A)
@Mathhead200 Of course...
I felt that the carelessly phrased question deserved a similarly phrased answer.
Jun 16, 2011 at 10:48am
Hey! Memory leaks are not funny fun2code... Okay, maybe a little... :)
Jun 16, 2011 at 4:15pm
Why would you do that? Just swap the data fields in the two nodes, assuming that's how your nodes are structured.
Jun 16, 2011 at 8:29pm
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.