开发者

How do I swap elements in a List?

开发者 https://www.devze.com 2022-12-09 13:39 出处:网络
I am trying to learn about data structures and algorithms on my own. I wrote my own double-linked list in C and now I want to write some algorithms to perform on the list. What is the preferred way to

I am trying to learn about data structures and algorithms on my own. I wrote my own double-linked list in C and now I want to write some algorithms to perform on the list. What is the preferred way to swap list items? Is it better to swap开发者_JAVA百科 the content or to rearrange the pointers which point to the next and previous list item?


Rearrange the pointers. Swapping the data items can have side effects. In particular, you may have stored a reference to a node somewhere outside of the function, and usually when you rearrange the order of nodes in a list, you don't want people holding a reference to a node to suddenly find that the node points to new data. That's because generally the important, identifying characteristic of a node is the data it points to not its position in the list.


The canonical swap is done via pointer rearranging, has no side effects and is of course faster:

void swap (node *a, node *b) {
    node *tmp;

    tmp = a;
    a = b;
    b = tmp;
}


Depending on the kind of content being stored in the elements of the linked list, swapping the actual content of the element would be tricky (think about a linked list of different length strings for instance) so therefore it's easier to swap the pointers which point to the next and previous list items.


Depends on how you've allocated the content.

If you're storing the pointer to the content, then switching the content isn't a big deal. If you have a big structure that's part of your node, then switching the pointers could be more efficient than copying the whole content.


I tend to side with what most people have already said. A bit of background will probably help: swapping pointers is guaranteed to work whereas swapping objects may not always be as simple as it looks. Think of temporaries that may/will be created and exceptions (and I mean in general and not a C++ language feature way) can occur and actually leave your container (list) in an undesirable state. Look for invariants in your container -- which is that a swap should leave the list size intact as well as the elements intact and design thereon.


Is it better to swap the content or to rearrange the pointers which point to the next and previous list item?

Rearrange the pointers, because it might be much more expensive to swap all the elements of the nodes.

Swapping two pointers is constant operation in terms of Time Complexity.

However, if you'd like to swap all the elements between two nodes, then you would need access them all, and swap them with their counterparts, which is O(f), where f is the number of fields the struct representing the node has (i.e. the number of data a node has).

In other words, swapping node data is a linear operation, while swapping two pointers is constant.


Swapping the data may have side effects, especially when you may have stored a reference to a node somewhere outside of the function, which is already covered in the accepted answer, but I wanted to point out that fact too.

0

精彩评论

暂无评论...
验证码 换一张
取 消