Thread: Removing odd indicies from linked list

  1. #1
    Registered User Ryan.'s Avatar
    Join Date
    Jul 2010
    Location
    Berkeley, CA
    Posts
    16

    Removing odd indicies from linked list

    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?
    Last edited by Ryan.; 07-10-2010 at 03:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's not a linked list. That's an array of integers.
    Code:
    memmove( list + offset, list + offset + skip, (listlen - offset - skip) * sizeof *list );
    memset( list + offset + skip, 0, skip * sizeof *list );
    That looks about right. Or, if you want to roll your own, use a for loop with the above for determining how many places you move, and copy them by hand.

    Long story short: You don't have a linked list, it's an array. "deleting" from an array means you shift the array elements around. No way around it.


    Quzah.
    Last edited by quzah; 07-10-2010 at 06:48 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Ryan.'s Avatar
    Join Date
    Jul 2010
    Location
    Berkeley, CA
    Posts
    16
    The actual problem I am dealing with in assembly is a linked list. I just made an array for modelling purposes because it is easier to translate into MIPS assembly. Assembly is rather annoying when it comes to this sort of stuff.

    That said, I think the above was way wrong.

    I am actually writing the assembly right now and it looks like I have to do this:

    - Copy contents of 2nd pair into BASE

    - Recursively go to address in current (i.e. BASE NEXT-->X) and set X's NEXT to X+1's NEXT, etc

    Like this, sort of:

    Code:
    void remOdd( )
    {
      if ( !list )
        return ;
    
      // Shift head
      list = list->next ;
      struct node* node = list ;
    
      // Relink
      while ( node )
        {
          if ( !node->next )
    	return ;
          
          node->next = ( node->next )->next ;
          node = node->next ;
        }
      
      return ;
    }
    Last edited by Ryan.; 07-10-2010 at 07:12 PM.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    What good is an inaccurate model? Prototyping can help sometimes, but remember, premature optimization is always bad.

    I don't know about MIPS, but I prefer x86 asm's handling of pointers to C's. They seem to be more common sense in asm. That should help since linked lists rely on a sturdy understanding on pointers.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If all you are doing is snipping out chunks, find the first node you want to keep:
    Code:
    while( keep->next != kepttail )
    {
        temp = keep->next;
        keep->next = keep->next->next; 
        free( temp );
    }
    I'm not sure why this is hard, just loop until it's lined up right.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Ryan.'s Avatar
    Join Date
    Jul 2010
    Location
    Berkeley, CA
    Posts
    16
    Isn't that pretty much what I am doing? Minus the free, since this is assembly.

    And there is no tail reference.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Ryan. View Post
    The actual problem I am dealing with in assembly is a linked list.
    Then don't post code for an array, as it will simply confuse others. The bold example you gave is sufficient to explain the problem.

    To perform optimisation of this kind, first start with the simple way of coding it that actually works. Next, once it works, think about improvements.
    Always make your improvements on a copy. Keep the orignal good working (but possibly slow) code around for several reasons:
    You can easily verify that they behave the same in all cases, and
    you have something to compare your optimised code to to know if it is in fact faster in any way.
    In case you haven't heard it before: "Get it working first, then get it fast".

    Now, why do you want to write any of this in assembly? From a performance and maintainability perspective that most likely isn't beneficial here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User Ryan.'s Avatar
    Join Date
    Jul 2010
    Location
    Berkeley, CA
    Posts
    16
    Quote Originally Posted by iMalc View Post
    Then don't post code for an array, as it will simply confuse others. The bold example you gave is sufficient to explain the problem.
    To quote myself: "That said, I think the above was way wrong."

    I was confused myself, hence...

    Quote Originally Posted by iMalc View Post
    To perform optimisation of this kind, first start with the simple way of coding it that actually works. Next, once it works, think about improvements.
    Always make your improvements on a copy. Keep the orignal good working (but possibly slow) code around for several reasons:
    You can easily verify that they behave the same in all cases, and
    you have something to compare your optimised code to to know if it is in fact faster in any way.
    In case you haven't heard it before: "Get it working first, then get it fast".
    Thanks for the advice. I actually had the linked list version laying around before I wrote the array version, and then when I found out the array version didn't work, well... I went back to the old, working copy.

    Quote Originally Posted by iMalc View Post
    Now, why do you want to write any of this in assembly? From a performance and maintainability perspective that most likely isn't beneficial here.
    It was for homework, which I have finished. I think at first I was overcomplicating the conversion of the linked list C code to assembly.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Ah right. I wonder then if there are any forums out there for MIPS assembly that would be even more useful.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM