Rekurencyjne odwracanie listy

0

Witam, pisze krotki program, ktory ma odwracac elementy listy(pierwszy z ostatnim, drugi z przedostatnim itd.) na 2 sposoby. Pierwszy udalo mi się zrobic, ale z wersja rekurencyjna mam pewne problemy. Bardzo prosze o pomoc w poprawieniu mojej funkcji i ew. sugestie jak moge ulepszyc dzialanie programu. Z gory dziekuje.

Oto kod:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>

struct element
{
char value;
struct element *next;
};

void flip(struct element *current, struct element *head, struct element *prev)
{
     struct element *tmp;
     
     if (head!=NULL)
     {
        prev=NULL;
        current=head;
        while (current!=NULL)
        {
              tmp=current->next;
              current->next=prev;
              prev=current;
              current=tmp;
        }
     }
}
void flipr(struct element *current, struct element *head, struct element *prev)
{
     
     struct element *tmp;
     int i;
     if ((head!=NULL)&&(i==1))
     {
        prev=NULL;
        current=head;
        i=0;
     }
     if (current!=NULL)
     {
        tmp=current->next;
        current->next=prev;
        prev=current;
        current=tmp;
        flipr(current,head,prev);
     }
     
}



int main()
{
    struct element *head, *prev, *current;
    char tab[]="abcdefghijklmnoprstuwxyz";
    int i;
    head=NULL;
       
    for (i=0;i<strlen(tab);i++)
    {
        
        current=(struct element*)malloc(sizeof(struct element));
        if (head==NULL)
        {
            head=current;
        }
        else
        {
            prev->next=current;
        }
        current->next=NULL;
        current->value=tab[i];
        prev=current;
    }
    
   
    flipr(current,head,prev);
current=prev;
  
    

    while (current!=NULL)
    {
    printf("%c",current->value);
    current=current->next;
    }    
system("PAUSE"); 
return 0; 
}

Dodam, ze kod musi byc zgodny z norma ANSI.
Funkcja flip, to funkcja dzialajaca, a flipr wymaga poprawek.
Dodam, ze gdzies znalazlem sugestie, ze wymaga to uzycia funkcji "owijajacej", wiem ze w Pascalu jest to mozliwe, ale ze w C to pierwsze slysze.

Pozdrawiam.

0

Listę dwukierunkową można odwrócić w czasie O(1).
Wystarczy dodać zmienną która określa kierunek w jakim przechodzi się listę.

1 użytkowników online, w tym zalogowanych: 0, gości: 1