Sunday, August 2, 2009

Hey !quite urgent.can anyone tell me a c program to reverse a linked list using recursive function??

I don't know, but when you do find out let me know.

Hey !quite urgent.can anyone tell me a c program to reverse a linked list using recursive function??
it has to use :


if your list L can be split as [L1;a;L2] where L1 and L2 are lists and a is an item, then





rev(L) = [rev(L2); a; rev(L1)]





f(rL2, [a;L1]) :-


f([a;rL2], L1)


f(rL2, []) = rL2.





or, more clearly (?), in C:


typedef struct list


{


int a;


list *l;


} list;





list reverse(list *X, list *Y)


{


list *X1, *Y1;


int a;





if(Y == NULL)


return(X); // X is already reversed


else


{


// Y is a sub-list plus its head : [a;Y1]


a = Y-%26gt;a;


Y1 = Y-%26gt;l;





// make X1 = [a;X], X is already reversed


X1 = (list *) malloc(sizeof(list)); // old-style C, now we create objets instead!


X1-%26gt;a = a;


X1-%26gt;l = X;


return(reverse(X1, Y1));


}


}





usage : reverse(NULL, L) returns the reverse of list L (or at least I believe so...)
Reply:a c docter


No comments:

Post a Comment