A dictionary is a data structure that represents a collection of keys and values.…Pairs of data items that have a one-to-one association.
In this post I show you how implement a very simple Dictionary with Key as integer and value as string.

Advantage: Very simple without using malloc function
disadvantages: Records have limitations that depend of defines MAXCOL and MAXROWl

 
#define MAXCOL 50
#define MAXROW 15

typedef struct {
  int Key;
  char Value[MAXCOL];
} DictionarySD;
DictionarySD dsd[MAXROW];

 //init structure
void InitSD()
{
   memset(dsd ,0,sizeof(dsd));//clear queue
}
/*read  item at index position, return -1 if index is overrange*/
int readIndex(int index,DictionarySD *items)
{
   int iret;
   iret=-1;
   if(index<MAXROW && index>-1)
  {
      *items=dsd[index];
      iret=index;
  }
  return iret;
}
/* read relative item, return index of item otherwise -1*/
int readValue(int Key,DictionarySD *items)
{
    int k;
    int iret;

    iret=-1;
    k=MAXROW;

    while(--k>=0)
        if(dsd[k].Key==Key)//it found
        {
            *items=dsd[k];//copy item
            iret=k;
        }
    return iret;
}
/*Remove a key return index of key removed othrwise -1*/
int RemoveKey(int Key)
{
    int iret=-1;
    DictionarySD item;
    int index;
    index=readValue(Key,&item);//finde item of key

    if (index>-1)//if it found
    {
       memset(&dsd[index],0,sizeof(dsd[index]));//fill zero
       iret=index;//return index removed
    }

    return iret;
}
//add an item into dictionary, return index of item addedded otherwise -1
int Addto(DictionarySD items)
{

    int ret;
    DictionarySD item;
    int index;

    ret=-1;
    index=readValue(items.Key,&item);//find free item

    if(index<0)//if univoche add into dictionary
    {
        index=readValue(0,&item);//find free item
        if(index>-1)
        {
            memcpy(&dsd[index],&items, sizeof(item));
            ret=index;
        }
        else
           ret= -2;//list of dictionary is full
    }

    return ret;
}

How to use it

void TestDictionary()
{
    int ret,k,i;
    DictionarySD item;
    InitSD();
    printf("****** Try to Add %d+(2) items ******** \n",MAXROW);
    for (k=0;k<(MAXROW+2);k++)
    {
        item.Key=10+k;
        sprintf(item.Value,"Hi I'm item key %d nice to meet you",item.Key);
        ret=Addto(item);
        printconsole (ret,item);
    }
    printf("\n****** Try to add exist items ******** \n");
    item.Key=15;
    strcpy(item.Value,"change an item");
    ret=Addto(item);
    printconsole (ret,item);
    printf("\n****** Try to remove an item ******** \n");
    ret=RemoveKey(15);
    if(ret>-1)printf("item removed at index %d\n",ret);
    else
       printf("Item not found\n");

    printf("\n****** Try to add an item ******** \n");
    item.Key=15;
    strcpy(item.Value,"Item changed");
    ret=Addto(item);
    printconsole (ret,item);
    printf("\n****** Read all keys ******** \n");
    for (k=0;k<(MAXROW);k++)
    {
        ret=readIndex(k,&item);
        if(ret>-1)
           printf("index:%2d, Key %d, Value: %s\n",ret,item.Key,item.Value);
        else
           printf("index %d not found\n",k);
    }

}

Result
Cattura

Show also:Queue of String and also: List of String

Advertisements