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 3596394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:58:56+00:00 2026-05-18T19:58:56+00:00

One of the things which I miss while writing programs in C is a

  • 0

One of the things which I miss while writing programs in C is a dictionary data structure. What’s the most convenient way to implement one in C? I am not looking for performance, but ease of coding it from scratch. I don’t want it to be generic either — something like char*→int will do. But I do want it to be able to store an arbitrary number of items.

This is intended more as an exercise. I know that there are 3rd party libraries available which one can use. But consider for a moment, that they don’t exist. In such a situation what’s the quickest way you can implement a dictionary satisfying the above requirements.

  • 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-05-18T19:58:57+00:00Added an answer on May 18, 2026 at 7:58 pm

    Section 6.6 of The C Programming Language presents a simple dictionary (hashtable) data structure. I don’t think a useful dictionary implementation could get any simpler than this. For your convenience, I reproduce the code here.

    struct nlist { /* table entry: */
        struct nlist *next; /* next entry in chain */
        char *name; /* defined name */
        char *defn; /* replacement text */
    };
    
    #define HASHSIZE 101
    static struct nlist *hashtab[HASHSIZE]; /* pointer table */
    
    /* hash: form hash value for string s */
    unsigned hash(char *s)
    {
        unsigned hashval;
        for (hashval = 0; *s != '\0'; s++)
          hashval = *s + 31 * hashval;
        return hashval % HASHSIZE;
    }
    
    /* lookup: look for s in hashtab */
    struct nlist *lookup(char *s)
    {
        struct nlist *np;
        for (np = hashtab[hash(s)]; np != NULL; np = np->next)
            if (strcmp(s, np->name) == 0)
              return np; /* found */
        return NULL; /* not found */
    }
    
    char *strdup(char *);
    /* install: put (name, defn) in hashtab */
    struct nlist *install(char *name, char *defn)
    {
        struct nlist *np;
        unsigned hashval;
        if ((np = lookup(name)) == NULL) { /* not found */
            np = (struct nlist *) malloc(sizeof(*np));
            if (np == NULL || (np->name = strdup(name)) == NULL)
              return NULL;
            hashval = hash(name);
            np->next = hashtab[hashval];
            hashtab[hashval] = np;
        } else /* already there */
            free((void *) np->defn); /*free previous defn */
        if ((np->defn = strdup(defn)) == NULL)
           return NULL;
        return np;
    }
    
    char *strdup(char *s) /* make a duplicate of s */
    {
        char *p;
        p = (char *) malloc(strlen(s)+1); /* +1 for ’\0’ */
        if (p != NULL)
           strcpy(p, s);
        return p;
    }
    

    Note that if the hashes of two strings collide, it may lead to an O(n) lookup time. You can reduce the likelihood of collisions by increasing the value of HASHSIZE. For a complete discussion of the data structure, please consult the book.

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

Sidebar

Related Questions

One of the things I miss the most in ActionScript is the lack of
One of the really nice things about python is the simplicity with which you
I just want to clarify one thing. This is not a question on which
What are all the things one needs to be careful about when coding in
I've heard that there are some things one cannot do as a computer programmer,
There seem to be a number of weird things one could do if one
One of the things that has been talked about a few times on the
One of the things that get me thoroughly confused is the use of session.Flush
One of the things that always bugs me about using Readers and Streams in
One of the things that the Microsoft documentation says about enabling reliable sessions is

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.