FidoNet Echomail Archive

<<< Previous Index Next >>>

From: Neil Heller
To: Jasen Betts
Date: 2004-08-28 18:45:00
Subject: Reversing a linked list

JB>as for the algorithm

JB>Immagine you were handed ap pack of cards and asked to reverse their

JB>  - take one from pile a, place it on pile b, repeat until pile a is

JB>the same thing can be done with linked lists there's just a 
JB>few pointers to keep track of.


That was the sound made by the heal of my hand hitting my forehead.    
That's an excellent explanation.

Why did you use pointers to pointers in your code below?  What advantage 
is gained?

JB>spoiler follows:

JB>/*  reverse a linked list

JB>   NOTE:       * * * U N T E S T E D * * *

JB> */

JB>void   reverselist (item **startp)
JB>  { item *stale=NULL,
JB>         *this=*startp,  /* get start of the list into this */
JB>         *fresh;

JB>    while(this)
JB>        {
JB>        fresh=this->next;     /* remember where the list is going */
JB>        this->next=stale;     /* point this item backwards        */
JB>        stale=this;           /* shuffle on to the next item  */
JB>        this=fresh;           /* ditto */
JB>        }
JB>     *startp=stale;  /* point the start to the new start */
JB>     }
JB>  }

JB>when you you call it you do  reverseliet( & somelist )
JB>that way it can update somelist to point to the other end of the
JB>list. (ie the new start of the list)

 CMPQwk 1.42 999

--- Maximus/2 3.01
 * Origin: COMM Port OS/2 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 633/267

<<< Previous Index Next >>>