![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0
![]() |
Hi,
I have a programming task - a cyclic list of chars. I have to reverse the elements of the list. But I don't know what this means ? Any ideas ? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
Assuming I understand the question properly (not entirely sure; sorry if this is a brainfart), a cyclic list is something like:
h e l l o That's just a list; what makes it cyclic is that if I was at the start of it and asked for the next element repeatedly, I'd get: h e l l o h e l l o h e ... and so on. Assuming this is actually correct, reversing this cyclic list is really the same as reversing any other list; the cyclic nature has more to do with how you look at it, rather than the data structure. o l l e h ( o l l e h o ...) is the reversed list. Great, but of course I've been ignoring the fact that your list is actually cyclic as stored (assuming again that I understand the question). So you have a linked list. You have a pointer to the 'current element' rather than a head pointer (since cyclic lists don't really have heads), and rather than having a NULL pointer off the tail of the linked list, you have a pointer back to the 'head'. (You could have a head pointer, too, if you fancy, but it would be a little arbitrary). Well, reversing a cyclic list as far as I can see is just like reversing any other linked list, if a little more mind bending. You start by making a copy of your 'current item' pointer so you'll know when you're finished, then you take a copy of the 'next' pointer on that item and swap the next and prev pointers. Then you follow the 'next' pointer you saved a copy of a minute ago and repeat the process until the 'next' pointer would take you back to the start, which is the copy of the 'current' pointer you saved before you started. You don't particularly have to worry about the head or tail of the list since they're only special in your head anyway; they get correctly reversed this way, too, since really they're the same as all the others. Hope this helps! |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0
![]() |
Thank you mackenga
this helps for sure |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 16
Rep Power: 0
![]() |
Reversing a cyclic list is pretty easy, you just switch the head and tail of the list.
---------------------- Help on programming (C/C++, Java, C# or any other programming language) for homework, assignments, course works and projects |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
Quote:
Suppose the following list head tail | | a -> b -> c -> d -> e -> (back to a) Producing: abcde Now switch the head and tail. Walk the head to the tail. Producing: ea tail head | | a -> b -> c -> d -> e -> (back to a) If it were a doubly linked list, you could walk the tail back to the head, but there is no guarantee from the original post that it is not an array of characters.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|