Best way to have an list of indices which changes order

This is a discussion on Best way to have an list of indices which changes order within the C Programming forums, part of the General Programming Boards category; Need some advice about how best to do this in C. I need a list of indices which specify the ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Best way to have an list of indices which changes order

    Need some advice about how best to do this in C.

    I need a list of indices which specify the order in which the elements of an array will be accessed for a specific purpose (the order of the accessed array must stay constant, it's only for this special case that its elements must be accessed in a variable order). This order will change in the following way: When one index is 'selected' it should move from its current position in the list to the first position.

    The number of elements is relatively small, i.e. probably < 15 in most cases.

    What's the best way to do this? As a linked list, or as an array of ints that gets rebuilt every time, or some other way?

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A linked list will always require you to pop a link and set a link to remove an item, and the same to add an item at the front, while an array will require you to move on average n/2 items each time.

    If n<15, I doubt that any efficiency here would be noticed by anyone, anywhere. In fact, you'd probably have to work pretty hard to come up with an algorithm that didn't finish more-or-less instantaneously. So it probably comes down to what ties in with the rest of your code the best (if this is the only linked list, it may not be worth the hassle, etc.)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Why not just exchange the value in the indecies array with its first one? Or did you mean that when an element is selected, all other elements must necessarily FOLLOW it in their same contiguous sequence, such as if your selection is part of a sort algorithm or some "most recently used" descending order list?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Yes exactly. "Most recently used" is what I'm after.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    In that case, if there are only 15, then an array of ints is adequate. Move the elements down one to fill the gap left by the 'selected' one. The 'selected' one then goes to the top.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll need to initialize the index array, before trying to use it:
    Code:
    for(i = 0; i < 15; i++)
      index[i] = i;
    Then all references to the other array, are done only *thru* the index array, and you can
    move the indices, around, to get your last used string's index to the top.

    (I'm just using stringarray[], as an example here).

    Code:
    for(i = 0; i < 15; i++)
       printf("&#37;s", stringarray[index[i]]);
    Last edited by Adak; 09-29-2008 at 12:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  4. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21