I am recently writing a doubly link list by C++
However,I don't know how to perform shuffle as the following:

Places all the nodes in odds positions before that in even ones.
For example,after executing shuffle, the list {a, b, c, d, e} will become {a, c, e, b, d}

Can anyone help me to write a function for this?
Thanks every brother!!:)

Recommended Answers

All 6 Replies

I am recently writing a doubly link list by C++
However,I don't know how to perform shuffle as the following:

Places all the nodes in odds positions before that in even ones.
For example,after executing shuffle, the list {a, b, c, d, e} will become {a, c, e, b, d}

Can anyone help me to write a function for this?
Thanks every brother!!:)

I have tried many times. But I am really confused with the pointers.
I don't know where to place the pointers and how to move them.

First of all play with pointers until you REALLY understand, if you are doing sufficient C++/Computer science failure to understand pointers, dereferencing etc. is going to leave you in tonnes of trouble. [The advantage of being old, is that we had to grok pointers, in assembly aged 10 (to crack the latest 32kilobyte game) :)]

If you are confused with pointers, then start small. Move just one item. If you can do that (and print out the stack). Then can you move two. Then can you move an item twice, and then three times etc.. then move one item a random number of times..

Post some code as you do each stage and get stuck and you will quickly get it [or decide to change major ;) ]

Place all the nodes in odd positions before those in even ones. For example, the list {a, b, c, d, e} will become {a, c, e, b, d}

Create two empty lists, listOdd and listEven. Create a bool "toggle" initialized to false. Loop through your original list, placing the element in listOdd or listEven according to "toggle". Toggle "toggle" and continue the loop. Rebuild the original list by appending listOdd and listEven.

Create two empty lists, listOdd and listEven. Create a bool "toggle" initialized to false. Loop through your original list, placing the element in listOdd or listEven according to "toggle". Toggle "toggle" and continue the loop. Rebuild the original list by appending listOdd and listEven.

Thanks for yout advice. Now I know what I need to do:icon_cheesygrin:

Create two empty lists, listOdd and listEven. Create a bool "toggle" initialized to false. Loop through your original list, placing the element in listOdd or listEven according to "toggle". Toggle "toggle" and continue the loop. Rebuild the original list by appending listOdd and listEven.

I have declare two pointers a and b.
a points to first node and link it to a pointer call "odd"
b points to second node and link it to pointer call "even"
than a moves to b->next
and b moves to a->next
and assign them to "odd" and "even" respectively.
This process repeats.

However, I don't know what is the condition to stop
when there even numbers of nodes in the link or
when there odd numbers of nodes in the link.

Thanks a lot

Well you know you have reached the end of the linked list if the pointer to the next is equal to NULL.

By the way, you can do this with only the original linked listed, so there is no need for extra data structues :) But that might be a task to acheive once you get a working solution.

Chri

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.