I want to implement a key value pair data structure in C. Any idea?
Asked
Active
Viewed 4.9k times
19
-
What are you mapping to what? What functions do you need? – Rafe Kettler Apr 14 '11 at 03:39
-
int to int mapping. EX 1 -> 2 3 -> 5 – Udara S.S Liyanage Apr 14 '11 at 03:49
-
https://stackoverflow.com/questions/4384359/quick-way-to-implement-dictionary-in-c – Ciro Santilli OurBigBook.com Aug 20 '17 at 18:32
3 Answers
12
This is a simple hash table implementation in ANSI C. It supports the rudimentary functions generally expected of a hash table:
- Inserting and retrieving key-value associations
- Querying the existence of a key
- Returning the total number of key-value associations
- Enumerating over all key-value associations
Hope this helps!

Carlos Daniel Gadea Omelchenko
- 601
- 1
- 8
- 19
-
10Actually, to be a bit pedantic, that is not ANSI C since it defines functions whose names start with "str". You can't do that, the entire space of global functions names that start with "str" is reserved. – unwind Apr 14 '11 at 09:06
-
I don't like it, because it doesn't specify th esource code of the functions, and it does not seem to expand or shrink. It just has a fixed number of items. – Jul 11 '20 at 01:30
10
If your key and value part are same data type then you can use a 2-D array with 2 column where first column will be your key and second will be data. IT will behave as map but time complexity will diff. Time complexity : search - O(n) Insert - want to maintain unique key then O(n) else O(1).
int map[N][2];
if you want to have key-value pair different type then you can use list structure.
struct node
{
int key; //key part
string value; // value part
struct node *next;
};
Time complexity : search - O(n) Insert - want to maintain unique key then O(n) else O(1).

sitaram chhimpa
- 471
- 7
- 12