mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
added a dictionary
I added a dictionary.
This commit is contained in:
parent
34e9ba11d3
commit
df5d9d63c3
49
Data Structures/dictionary/README.md
Normal file
49
Data Structures/dictionary/README.md
Normal file
@ -0,0 +1,49 @@
|
||||
## Dictionary
|
||||
|
||||
This is simple and generic dictionary. You can instantiate multiple dictionaries with
|
||||
the constructor. See interface below.
|
||||
|
||||
Each dictionary has space for 1000 elements.
|
||||
|
||||
You need add the files **dic.c** and **dic.h** in your project directory.
|
||||
After that you include dic.h
|
||||
|
||||
### Overview about functions
|
||||
|
||||
``` c
|
||||
Dictionary * create_dict(void);
|
||||
```
|
||||
create_dict: is a simple constructor for creating
|
||||
a dictionary and setting up the
|
||||
member field 'number_of_elements'
|
||||
and prepares the inner array 'elements'
|
||||
|
||||
``` c
|
||||
int add_item_label(Dictionary *,char label[],void *);
|
||||
```
|
||||
add_item_label: adds item (void*) to the dictionary at given label
|
||||
returns 0 if adding was sucessful otherwise -1
|
||||
|
||||
|
||||
``` c
|
||||
int add_item_index(Dictionary *, int index, void *);
|
||||
```
|
||||
add_item_index: adds item (void*) to the dictionary at given index (int)
|
||||
returns 0 if adding was sucessful otherwise -1
|
||||
|
||||
``` c
|
||||
void * get_element_label(Dictionary *, char []);
|
||||
```
|
||||
get_element: returns the element at given label
|
||||
|
||||
|
||||
``` c
|
||||
void * get_element_index(Dictionary *, int);
|
||||
```
|
||||
get_element: returns the element at given index
|
||||
|
||||
|
||||
``` c
|
||||
void destroy(Dictionary *);
|
||||
```
|
||||
simple destructor function for avoiding memory leaks.
|
109
Data Structures/dictionary/dict.c
Normal file
109
Data Structures/dictionary/dict.c
Normal file
@ -0,0 +1,109 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "dict.h"
|
||||
|
||||
|
||||
/* simple constructor */
|
||||
Dictionary * create_dict(void)
|
||||
{
|
||||
Dictionary * p_dic = malloc(sizeof (Dictionary));
|
||||
if (p_dic)
|
||||
{
|
||||
p_dic->number_of_elements = 0;
|
||||
|
||||
/* initializes the elemens of the array with NULL-pointer */
|
||||
for (int i = 0; i < MAXELEMENTS; i++)
|
||||
{
|
||||
p_dic->elements[i] = NULL;
|
||||
}
|
||||
|
||||
return p_dic;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("unable to create a dictionary\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
utility function
|
||||
sdbm hash algorithm
|
||||
returns a hashcode for the given string 's'
|
||||
*/
|
||||
int get_hash(char s[])
|
||||
{
|
||||
unsigned int hash_code = 0;
|
||||
|
||||
/* iterates over string at each character */
|
||||
for (int counter = 0; s[counter]!='\0'; counter++)
|
||||
{
|
||||
/* actual computing of the hash code */
|
||||
hash_code = s[counter] + (hash_code << 6) + (hash_code << 16) - hash_code;
|
||||
}
|
||||
|
||||
/* % modulo is for fitting the index in array. */
|
||||
return hash_code % MAXELEMENTS;
|
||||
}
|
||||
|
||||
|
||||
int add_item_label(Dictionary * dic,char label[],void * item)
|
||||
{
|
||||
unsigned int index = get_hash(label);
|
||||
|
||||
/* make sure index is fitting */
|
||||
if (index < MAXELEMENTS)
|
||||
{
|
||||
dic->elements[index] = item;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* error case */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int add_item_index(Dictionary * dic , int index, void * item)
|
||||
{
|
||||
/* make sure whether this place is already given */
|
||||
if (!dic->elements[index])
|
||||
{
|
||||
dic->elements[index] = item;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* error case */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void * get_element_label(Dictionary * dict, char s[])
|
||||
{
|
||||
int index = get_hash(s);
|
||||
if (dict->elements[index])
|
||||
{
|
||||
return dict->elements[index];
|
||||
}
|
||||
|
||||
printf("None entry at given label\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void * get_element_index(Dictionary * dict, int index)
|
||||
{
|
||||
if (index >= 0 && index < MAXELEMENTS)
|
||||
{
|
||||
return dict->elements[index];
|
||||
}
|
||||
|
||||
printf("index out of bounds!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void destroy(Dictionary * dict)
|
||||
{
|
||||
free(dict);
|
||||
}
|
68
Data Structures/dictionary/dict.h
Normal file
68
Data Structures/dictionary/dict.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
public interface for the dictionary.
|
||||
|
||||
The dictionary prepares space for 1000 elements.
|
||||
*/
|
||||
|
||||
#ifndef __DICT__H
|
||||
#define __DICT__H
|
||||
|
||||
#define MAXELEMENTS 1000
|
||||
|
||||
/*
|
||||
special data type called 'Dictionary'
|
||||
for generic use
|
||||
*/
|
||||
typedef struct Dict
|
||||
{
|
||||
/*
|
||||
void* array for generic use of the dictionary.
|
||||
there actual saves the entries.
|
||||
*/
|
||||
void * elements[MAXELEMENTS];
|
||||
|
||||
/* contains the number of elements in this dictionary */
|
||||
int number_of_elements;
|
||||
|
||||
} Dictionary;
|
||||
|
||||
/*
|
||||
create_dict: is a simple constructor for creating
|
||||
a dictionary and setting up the
|
||||
member field 'number_of_elements'
|
||||
and prepares the inner array 'elements'
|
||||
*/
|
||||
Dictionary * create_dict(void);
|
||||
|
||||
/*
|
||||
add_item_label: adds item (void*) to the dictionary at given label
|
||||
returns 0 if adding was sucessful otherwise -1
|
||||
*/
|
||||
int add_item_label(Dictionary *,char label[],void *);
|
||||
|
||||
/*
|
||||
add_item_index: adds item (void*) to the dictionary at given index (int)
|
||||
returns 0 if adding was sucessful otherwise -1
|
||||
*/
|
||||
int add_item_index(Dictionary *, int index, void *);
|
||||
|
||||
|
||||
/*
|
||||
get_element: returns the element at given label
|
||||
*/
|
||||
void * get_element_label(Dictionary *, char []);
|
||||
|
||||
|
||||
/*
|
||||
get_element: returns the element at given index
|
||||
*/
|
||||
void * get_element_index(Dictionary *, int );
|
||||
|
||||
/*
|
||||
simple destrcutor function
|
||||
*/
|
||||
void destroy(Dictionary *);
|
||||
|
||||
|
||||
#endif
|
49
Data Structures/dictionary/test_program.c
Normal file
49
Data Structures/dictionary/test_program.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
This is a simple test program for the dictionary.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* includes the dictionary */
|
||||
#include "dict.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Dictionary * testObj1;
|
||||
Dictionary * testObj2;
|
||||
|
||||
int value = 28;
|
||||
|
||||
testObj1 = create_dict();
|
||||
testObj2 = create_dict();
|
||||
|
||||
add_item_label(testObj1,"age",&value);
|
||||
add_item_label(testObj2,"name","Christian");
|
||||
|
||||
|
||||
/*
|
||||
test for function add_item_label
|
||||
|
||||
attention:
|
||||
The void* pointer must be convert into an int* pointer.
|
||||
After that you can dereference it.
|
||||
*/
|
||||
printf("My age is %d\n",*((int *)get_element_label(testObj1,"age")));
|
||||
printf("My name is %s\n",get_element_label(testObj2,"name"));
|
||||
|
||||
/* test for function add_item_index */
|
||||
if (!add_item_index(testObj1,0,&value))
|
||||
{
|
||||
printf("My age at index %d is %d\n",0,*((int *)get_element_index(testObj1,0)));
|
||||
}
|
||||
|
||||
/* error scenario */
|
||||
/* get_element_label(testObj,"none"); */
|
||||
|
||||
/* tidy up */
|
||||
destroy(testObj1);
|
||||
destroy(testObj2);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user