Hello...
I have a solution to this problem, but I think it is too complicated to be optimal. I need it as simple as possible before I start coding it in assembly.
Here is what I am given (v's are values and A's are addresses pointing to the next pair):
LIST--> (v1, A1-)->(v2, A2-)->(v3,A3-)->(v4,A4-)->(v5,A5-)->(v6,A6-)->(v7,A7-)-> etc
I need to "remove" or "re-link" the list to make it look like:
LIST--> (v2,A3-)->(v4,A5-)->(v6,A7-)->etc
(The first pair being indexed at "1", with the last address in the list being NULL.)
Anyway, I tend to overcomplicate things. Here is my C code (after many revisions):
Code:
int* remOdd( int* l )
{
int addr_pos = 1 ;
int swap_pos = 3 ;
// Entire list or first address is NULL
if ( !l || !*( l + addr_pos ) )
{
l = NULL ;
return l ;
}
// Shift to right
l = l + 2 ;
// Address is not NULL
while ( *( l + addr_pos ) )
{
swap_pos = addr_pos + 2 ;
// List ends on next address
if ( !*( l + swap_pos ) )
return l ;
// Swap addresses
*( l + addr_pos ) = *( l + swap_pos ) ;
addr_pos += 4 ;
}
return l ;
}
/* Somewhere else... */
linked_list = remOdd( linked_list ) ;
I am particularly concerned about the fact that I have to shift the list and reassign from somewhere outside of the function. (Which I may not have to do, but I couldn't figure out any other way...)
I would like to make it one big recursive function if at all possible.
Anybody know if there is any way to make this notably simpler?