banner



How To Use Dictionary In C#

Implement Lexicon in C

This article volition demonstrate multiple methods nigh how to implement a dictionary in C.

Apply hcreate, hsearch and hdestroy to Implement Dictionary Functionality in C

More often than not, the C standard library does non include a built-in lexicon data structure, but the POSIX standard specifies hash table management routines that can exist utilized to implement lexicon functionality. Namely, hcreate, hsearch and hdestroy provide the features like creating a hash tabular array, inserting items into it, searching the detail in a tabular array, and deallocating the whole data construction. Even though the implemented functionality is the blank minimum, information technology can solve many problem scenarios.

At first, the hcreate function needs to be called to create a hash table with a given maximum number of elements, which is passed as the only argument. Notation that capacity can't exist modified after the hcreate call; thus, the user needs to cistron this result into the code structure. One time the tabular array is created, the hsearch function tin be chosen to add items into it. It takes two arguments; the first i is of type ENTRY, defined as struct of char* for the key and void* for respective data. The second parameter is of blazon ACTION, which is essentially an enum consisting of ii elements, Detect and ENTER. The latter values are used to indicate what operation to deport on the table.

              #include <stdio.h> #include <stdlib.h> #include <search.h>  static char *companies[] = { "Intel", "AMD", "ARM", "Apple",                             "Marvell", "Qualcomm", "IBM", "Nvidia" };  static char *uarch[] = { "Willow Cove", "Zen 3", "A78", "A14",                             "ThunderX2", "Kryo", "z15", "Ampere" };  int principal(void) {    ENTRY eastward;    ENTRY *ep;     const size_t chapters = sizeof companies / sizeof companies[0];    hcreate(capacity);     for (size_t i = 0; i < capacity - 2; i++) {        eastward.fundamental = companies[i];        e.data = (void *) uarch[i];         ep = hsearch(east, ENTER);         if (ep == NULL) {            fprintf(stderr, "entry failed\n");            exit(EXIT_FAILURE);        }    }     e.fundamental = "Intel";    ep = hsearch(due east, Observe);    printf("%s -> %s\n", e.key, (char*)ep->data);     hdestroy();    go out(EXIT_SUCCESS); }                          

Output:

              Intel -> Willow Cove                          

Observe that the code examples demonstrate the key-value pairs represented by clear text, just the user may utilise hashing algorithms to create keys and shop those in the table. In this instance, we added 6 items to the table using the uncomplicated iteration and skipped the last two elements. The adjacent for loop, though, queries every item from the original array and prints the corresponding data.

The hdestroy function is used to free hash table retentiveness, simply information technology does not free the buffers in the ENTRY objects, which is usually created by the user. Thus, if pointers in these objects betoken to dynamic retentivity, the caller is responsible for keeping the handles for them and deallocating before the programme exit. Mind that individual entries can not exist deleted from the table.

              #include <stdio.h> #include <stdlib.h> #include <search.h>  static char *companies[] = { "Intel", "AMD", "ARM", "Apple tree",                             "Marvell", "Qualcomm", "IBM", "Nvidia" };  static char *uarch[] = { "Willow Cove", "Zen iii", "A78", "A14",                             "ThunderX2", "Kryo", "z15", "Ampere" };  int main(void) {    ENTRY e;    ENTRY *ep;     const size_t chapters = sizeof companies / sizeof companies[0];    hcreate(capacity);     for (size_t i = 0; i < capacity - 2; i++) {        e.key = companies[i];        due east.data = (void *) uarch[i];         ep = hsearch(e, ENTER);         if (ep == NULL) {            fprintf(stderr, "entry failed\due north");            exit(EXIT_FAILURE);        }    }     for (size_t i = 0; i < capacity; i++) {        e.primal = companies[i];        ep = hsearch(e, FIND);         ep ? printf("%s -> %s\n", east.key, (char*)ep->information) :            printf("%southward -> %southward\n", e.key, "Entry non found");    }     hdestroy();    get out(EXIT_SUCCESS); }                          

Output:

              Intel -> Willow Cove AMD -> Zen 3 ARM -> A78 Apple -> A14 Marvell -> ThunderX2 Qualcomm -> Kryo IBM -> Entry not found Nvidia -> Entry non found                          
Ezoic

How To Use Dictionary In C#,

Source: https://www.delftstack.com/howto/c/dictionary-in-c/

Posted by: morelandshrem1977.blogspot.com

0 Response to "How To Use Dictionary In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel