If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Swap adjacent nodes in doubly linked list?

I have been trying to swap two adjacent nodes for my linked list sort. Its not meant to be optimal, just meant to work. My problem is I either lose nodes or get Access Violation errors, Can anyone help?

Re: Swap adjacent nodes in doubly linked list?

Originally Posted by Sinlak

I have been trying to swap two adjacent nodes for my linked list sort. Its not meant to be optimal, just meant to work. My problem is I either lose nodes or get Access Violation errors, Can anyone help?

Before doing anything, did you draw on paper with boxes and arrows showing how the sort will work? You always do that first before writing any code. If you did that, then the code should follow the plan you drew up and work correctly on the first, second, or third try. If not, then you debug the code see where it diverges from your plan, or you then determine that your original plan was flawed.

If you're just writing code thinking how it should work without any plan on paper, then that is the wrong approach.

Re: Swap adjacent nodes in doubly linked list?

Originally Posted by Paul McKenzie

Before doing anything, did you draw on paper with boxes and arrows showing how the sort will work? You always do that first before writing any code. If you did that, then the code should follow the plan you drew up and work correctly on the first, second, or third try. If not, then you debug the code see where it diverges from your plan, or you then determine that your original plan was flawed.

If you're just writing code thinking how it should work without any plan on paper, then that is the wrong approach.

Regards,

Paul McKenzie

Ive been drawing all day, still Im at a loss. Everytime I recode it I feel like "This time it will work for sure!' then I hit F5 and my dreams are crushed!

Re: Swap adjacent nodes in doubly linked list?

Can you provide the definition of Node. What exactly is List::sortlist trying to do? as you do a data comparision first then enter a while loop which does more comparisons? You say that the payload (data) may be huge - yet you are doing comparisons on the data - so this data must be of a type for which the > and < operators have been defined.

Is this a homework assignment? If it's not then why bother with writing linked lists? Why not use one of the STL containers? If you do need to write your own list code, why not create the list sorted in the first place? As Paul mentioned in post #5, why store a huge payload of data in the linked list instead of just storing a pointer in the node to a dynamically created object?

Have you tried drawing a list of a few nodes on paper and then using the paper list walk through your code moving pointers etc on the paper exactly as the code says? I found in the past that helped me with pointer issues.

Re: Swap adjacent nodes in doubly linked list?

Originally Posted by 2kaud

Have you tried drawing a list of a few nodes on paper and then using the paper list walk through your code moving pointers etc on the paper exactly as the code says? I found in the past that helped me with pointer issues.

This is really what you need to do. Once you understand and can visualize the steps, coding it should be relatively easy. You need to figure out which nodes are involved and which pointers need to be swapped.

Re: Swap adjacent nodes in doubly linked list?

Originally Posted by Sinlak

The problem is that the payload may be HUGE so it would be better to swap the pointers around, thats why I have to solve it this way

Then, as has been suggested, one option is to store object pointers in the list nodes rather than the objects directly. An alternative if you don't want to use pointers is to store the objects in a random access array and then keep their index positions in the list.

Yet another alternative is to break up the linked list and store the node pointers in an array, Then you sort the array and afterwards you rebuild the list from the sorted nodes in the array. Sorting an array is more efficient than sorting a linked list and you get rid of the complicated node swaps. Java does this when sorting a LinkedList. It really sorts an ArrayList.

Re: Swap adjacent nodes in doubly linked list?

Just a further thought. Before using List::sortList are you sure that the links are correct? Have you verified them? Have you printed the list from head to tail and from tail to head? If the links are not correct before you call List::sortList then you will definately get problems!

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.