Advertisement

Reversing the order of values in a linked list

Started by April 15, 2001 02:06 AM
2 comments, last by paulcoz 23 years, 9 months ago
Can somebody see what's wrong with this code? I need to reverse the order of the values in this linked list. class vtx { double x,y,z; vtx* n; vtx* p; }; vtx* fvtx; // first vertex in linked list vtx* lvtx; // last vertex in linked list vtx* nvtx; // next vertex for while looping vtx* tvtx; // temp vertex Assume that there are an unknown no. of vertices in the list. fvtx->p = NULL, lvtx->n = NULL, and all the other vertices are linked correctly. Here's the code: lvtx->n = lvtx->p; lvtx->p = NULL; // since the last vertex will be the first, add NULL to previous nvtx = lvtx->n; // set to the second last vertex while (nvtx != NULL) // should eventually reach first vertex NULL { tvtx = nvtx->n; // store next nvtx->n = nvtx->p; switch previous to next nvtx->p = tvtx; // switch next to previous nvtx = nvtx->n; } tvtx = fvtx; // store first fvtx = lvtx; // switch last to first lvtx = tvtx; // switch first to last Can somebody spot the problem, or think of a better way of doing this (I don't really want to create a new list, then attach that - any other suggestions accepted graciously). Paulcoz. Edited by - paulcoz on April 15, 2001 3:10:46 AM
hey, that''s a really neat idea! I never thought of that...
i''ve always just popped all of the values off the top of one list onto the top of another, then switched ''em and deleted the first.

ok, so, basically just go and swap all of the previouses and nexts, then swap the first and last, right?

*checks for bugs*
oh, i think i see it:
you''re while''s not really traversing
it''s just repeatadly swapping the contents of the last (first now?) node

2 ways to fix this, decide how you like it better:
1) add "nvtx = nvtx->n; " as the last line in the while loop
or
2) use this initialization and loop:
nvtx = lvtx; // set to the second last vertex

while ((nvtx = nvtx->n) != NULL) // should eventually reach first vertex NULL
{
tvtx = nvtx->n; // store next
nvtx->n = nvtx->p; switch previous to next
nvtx->p = tvtx; // switch next to previous
}


i didn''t test it, but i''m sure that''d work...
lemino how it works out, and if that was the problem, k?




Thank you for your bandwidth.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~Succinct Demos Online~

"&ltDiLLiGaS> I''m suprised nobody takes M$ to court for rape... The OS keeps going down on you w/o your permission."
-- Succinct(Don't listen to me)
Advertisement
Interesting...

I just tested this out, and it works fine. Are you sure your linked-list is assembled correctly? What kind of errors were you getting?

If I'm not mistaken, your linked list is supposed to look like:

A -> B -> C -> D

where A is the first, and D is the last vertex and '->' is the next pointer.

Edited by - soulburn on April 15, 2001 4:01:34 AM
Sorry, I made a stupid typo elsewhere - the code is o.k. after all!!

Paulcoz.

This topic is closed to new replies.

Advertisement