Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 31st, 2005, 5:17 AM   #1
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
Question How can I reverse the elements of a cyclic list?

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 ?
jonyzz is offline   Reply With Quote
Old Aug 31st, 2005, 6:13 AM   #2
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
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!
mackenga is offline   Reply With Quote
Old Aug 31st, 2005, 8:38 AM   #3
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
Thank you mackenga this helps for sure
jonyzz is offline   Reply With Quote
Old Aug 31st, 2005, 9:12 AM   #4
proghelper
Newbie
 
Join Date: Jun 2005
Posts: 16
Rep Power: 0 proghelper is on a distinguished road
Lightbulb

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
proghelper is offline   Reply With Quote
Old Aug 31st, 2005, 11:49 AM   #5
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0 L7Sqr is an unknown quantity at this point
Quote:
Reversing a cyclic list is pretty easy, you just switch the head and tail of the list.
Careful with your advice...
Suppose the following list
head                tail
 |                   |
 a -> b -> c -> d -> e -> (back to a)
To print the list walk the head to the tail.
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]
L7Sqr is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC