[LCP]linked list question

Joachim Bauernberger bj at gmx.net
Tue Apr 2 18:11:05 UTC 2002


Hi,

I have a problem with recursively reversing the items in a linked list. 
The list items were first pushed into the list at the "head end" like 
this:

void push(mbody_t *mes) {

 int firstrun=1; 
 mbody_t *first = NULL;

    while(foo) {
      if (!firstrun) first=mes;
      ....
      .....
      firstrun=0;
      mes->bnext = first;
    }
}

Now I want to reverse the listitems in order to have the last item 
first, etc ...

I have written this function that is supposed to do that:

static void
rreverse(mbody_t **mb)
{
    mbody_t *first;
    mbody_t *last;

    if (*mb==NULL) return;
    first = *mb;
    last= first->bnext;
    if (last==NULL) return;
    rreverse(&last);
    first->bnext->bnext = first;
    first->bnext = NULL;
    *mb = last;
    return;
}

Now the problem ist that _only_ the last item in the list gets moved to 
the first position and consecutive items are not processed anymore.

mbody_t looks like this:
typedef struct mbody_t
{
    char *data;
    char *dsum;                   
    ....
    struct mbody_t *bnext;     
}
mbody_t;

I can't find the problem. Can somebody tell me what I am missing?

Thanks & Regards,
Joachim




-- 
Disclaimer:
By sending an email to ANY of my addresses you are agreeing that: 
1) I am by definition, "the intended recipient" 
2) All information in the email is mine to do with as I see fit and 
make such financial profit, political mileage, or good joke as it lends 
itself to. In particular, I may quote it on usenet. 
3) I may take the contents as representing the views of your company. 
4) This overrides any disclaimer or statement of confidentiality that 
may be included on your message. 





More information about the linuxCprogramming mailing list