Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:53:17+00:00 2026-06-18T09:53:17+00:00

I abstracted out a sorted list that I need to keep in C. One

  • 0

I abstracted out a sorted list that I need to keep in C. One way is best for reading and the other for writing.

WRITE: search KeyNumeric then KeyAlpha and write *Data

Key1 : [ KeyA, *Data1A, KeyB, *Data1B, KeyC, *Data1C ]
Key2 : [ KeyA, *Data2A, KeyB, *Data2B, KeyC, *Data2C ]
Key3 : [ KeyA, *Data3A, KeyB, *Data3B, KeyC, *Data3C ]


READ: search KeyAlpha then KeyNumeric and read *Data

KeyA : [ Key1, *Data1A, Key2, *Data2A, Key3, *Data3A ]
KeyB : [ Key1, *Data1B, Key2, *Data2B, Key3, *Data3B ]
KeyC : [ Key1, *Data1C, Key2, *Data2C, Key3, *Data3C ]

Does anyone recognize what would be the most efficient way to represent this data structure in memory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:53:17+00:00Added an answer on June 18, 2026 at 9:53 am

    If I understand correctly:

    • your data has a composite key that consists of a number and some kind of alphabetic (you don’t say if it’s a character or a string).
    • Sometimes you have the alpha-key, and need to search for the numeric, and sometimes vice-versa (it happens to be read and (over)write, but that’s beside the point, probably).
    • Insert and delete are rare but need to be supported.

    I’m also going to assume that the data keys are sparse, so a straight “[N][A]” array is not going to work for you.

    Since you want the data to be double indexed, I’d suggest that you need some kind of linked structure: either a list or a tree.

    To do it with linked lists, your C structure might look like this:

    struct stuff {
      int num_key;
      char alpha_key;
    
      /* The number-first lists.  */
      struct {
        struct stuff *next_num;
        struct stuff *next_alpha;
      } num_list;
    
      /* The alpha-first links.  */
      struct {
        struct stuff *next_alpha;
        struct stuff *next_num;
      } alpha_list;
    
      struct data Data;
    };
    

    So, if you have data items 1A, 1B, 1C, 2A, 2B, 2C, 3A, 3B, 3C these links would work like this:

    • 1A num_list.next_num points to 2A.
    • 1A num_list.next_alpha points to 1B.
    • 1A num_alpha.next_alpha points to 1B.
    • 1A num_alpha.next_num points to 2A.
    • 2B num_list.next_num is NULL.
    • 2B num_list.next_alpha points to 2C.
    • 2B num_alpha.next_alpha is NULL.
    • 2B num_alpha.next_num points to 3B.

    So, in words, num_list.next_num always points to something with the next number, but the first letter available. Similarly, alpha_list.next_alpha always points to something with the next letter, but the first number available. If you’re not looking at the head of the secondary list then pointer for the primary list is NULL because you never want to traverse the data that way, and maintaining a real pointer there would either cause bugs, or cause extra maintenance on insert or delete.

    You can think of it as two lists of lists:

    • num_list.next_num is a list of the heads of the num_list.next_alpha lists.
    • aplha_list.next_alpha is a list of the heads of the alpha_list.next_num lists.

    To find an item, you first move across one of the primary lists, num_list.next_num or aplha_list.next_alpha, and then down one of the secondary lists, num_list.next_alpha or num_alpha.next_num.


    So, clearly there are some efficiency issues with this:

    • malloc of all these little data blocks is inefficient.
    • lists are O(n) to access.

    If you are dealing with large quantities of data I would do two things:

    1. Use some kind of balanced tree instead of flat lists. The ‘heads of the lists’ then becomes the ‘roots of the trees’.

    2. Allocated a fixed-sized array of struct stuff and use array indexes as the links, instead of pointers. Then simply maintain a “free list” of unused slots. If your data out-grows the array then use realloc or allocate a second memory block and remember which indexes lie in which block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out the best way to perform a nested for-each
I currently have a client that wants me to 'abstract' out a domain model
I'm new to regex and have been struggling with one, and have abstracted my
I think I finally figured out they need to use this DeclarativeFieldsMetaclass (to turn
I've written an application that uses some serial port hardware directly, and abstracted the
A simplified example here: I have a game that I am writing in opengl
I want to created a generic sorted linked list. Thus I have an abstract
I have a library that is abstracted and basically looks like this: A.h namespace
I would like to abstract out pure SQL calls in my code as much
Might be my question is abstract or out of context, but i am asking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.