C programming language does not provide any direct implementation of Map or Dictionary Data structure. However, it doesn’t mean we cannot implement one. In C, a simple way to implement a map is to use arrays and store the key-value pairs as elements of the array.
How to implement Map in C?
One approach is to use two arrays, one for the keys and one for the values, where the key at index i in the key array corresponds to the value at index i in the value array.
- Variables used to implement Map in C
- This implementation uses two arrays, keys and values, to store the key-value pairs.
- The maximum number of elements in the map is defined as MAX_SIZE.
- The size variable keeps track of the current number of elements in the map.
- Functions used to implement Map in C
- The getIndex function searches for a key in the keys array and returns its index if found, or -1 if not found.
- The insert function inserts a key-value pair into the map. If the key already exists, it updates the value.
- The get function returns the value of a key in the map, or -1 if the key is not found.
- The printMap function prints all the key-value pairs in the map.
- In the main function, we insert some key-value pairs into the map, get the value of some keys, and print the map.
Here’s an example implementation of a map using arrays:
C
#include <stdio.h> #include <string.h> #define MAX_SIZE \ 100 // Maximum number of elements in the map int size = 0; // Current number of elements in the map char keys[MAX_SIZE][100]; // Array to store the keys int values[MAX_SIZE]; // Array to store the values // Function to get the index of a key in the keys array int getIndex( char key[]) { for ( int i = 0; i < size; i++) { if ( strcmp (keys[i], key) == 0) { return i; } } return -1; // Key not found } // Function to insert a key-value pair into the map void insert( char key[], int value) { int index = getIndex(key); if (index == -1) { // Key not found strcpy (keys[size], key); values[size] = value; size++; } else { // Key found values[index] = value; } } // Function to get the value of a key in the map int get( char key[]) { int index = getIndex(key); if (index == -1) { // Key not found return -1; } else { // Key found return values[index]; } } // Function to print the map void printMap() { for ( int i = 0; i < size; i++) { printf ( "%s: %d\n" , keys[i], values[i]); } } int main() { insert( "Geeks" , 5); insert( "GFG" , 3); insert( "neveropen" , 7); printf ( "Value of complete Map: \n" ); printMap(); printf ( "\nValue of apple: %d\n" , get( "GFG" )); printf ( "Index of neveropen: %d\n" , getIndex( "neveropen" )); return 0; } |
Value of complete Map: Geeks: 5 GFG: 3 neveropen: 7 Value of apple: 3 Index of neveropen: 2
This implementation is simple and easy to understand, but it has some limitations. One limitation is that the keys must be unique, otherwise the insert function will overwrite the value of an existing key. Another limitation is that the maximum size of the map is fixed, and if we exceed the maximum size, we cannot insert any more elements into the map.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!