Skip to content

Instantly share code, notes, and snippets.

@xfbs
Created March 5, 2017 20:07
Show Gist options
  • Select an option

  • Save xfbs/6e4a91b7939be598eea63a4ffaa3cc50 to your computer and use it in GitHub Desktop.

Select an option

Save xfbs/6e4a91b7939be598eea63a4ffaa3cc50 to your computer and use it in GitHub Desktop.
struct Dictionary {
char *prefix;
bool isWOrd;
Dictionary *arrayOfDictonary[];
};
/**
* Creates a new Dictionary object.
*/
Dictionary*
Dictionary_create();
/**
* Deletes a Dictionary object.
*/
void
Dictionary_delete( Dictionary* dict );
/**
* Inserts a word into the dictionary.
*/
void
Dictionary_insert( Dictionary* dict, const char* word )
{
//wennn dict auf nix zeigt musst du eerrstellen
const char* pointer = word;
//einfügen in leeren Knoten
if(dict->prefix == NULL){
dict->prefix= word;
dict ->isWord = true;
return;
}
//abgleich der beiden Wörter
int i= 0;
while(*(dict->prefix) != '/0' && (*pointer == '/0')&& *pointer==*(dict->prefix+i)){
pointer++;
i++;
}
//Wenn prefix = word => es muss nix eingefügt werden
if(*pointer==*(dict->prefix+i)){
dict->isWord = true;
return;
}
//wenn prefix teilwort von word ist => NOCHMAL ÜBERDENKEN
if(*dict->prefix=='/0'){
Dictonary *newEdge = Dictonary_create();
//da allocierter speicher verworfen wird muss auch eleganter gehen
free(newEdge->arrayOfDictionary);
newEdge->arrayOfDictionary = dict->arrayOfDictionary;
dict->arrayOfDictionary = malloc{sizeof(Dictonary*) * 26};
dict->arrayOfDictionary[letterToInt(dict->prefix + i)]= newEdge;
newEdge->prefix = dict->prefix;
newEdge->isWord = dict->isWord;
dict->prefix = word;
dict->isWord = true;
return;
}
//Wenn word teilwort von prefix ist => EInfügen im ast tiefer
if(*pointer == '/0'){
Dictonary_insert(dict->arrayOfDictionary[letterToInt(*pointer)],word);
return;
}
//erzeuge knoten und hänge beide darunter
Dictonary *newEdge = Dictonary_create();
newEdge->prefix= dict-> prefix;
newEdge->isword = dict->isWord;
free(newEdge->arrayOfDictionary);
newEdge->arrayOfDictionary = dict->arrayOfDictionary;
dict->arrayOfDictionary = malloc(sizeof(Dictionary*)*26);
dict->arrayOfDictonary[letterToInt(dict->prefix + i)]= newEdge;
dict->arrayOfDictonary[letterToInt(*pointer)]=dictonary_insert(Dictonary_create(),word);
dict->prefix = getLetters(i,word);
dict->isWord = false;
return;
}
int letterToInt(char let){
int posittionOfA = 65;
return ((int)let )-positionOfA;
}
char* getLetters(int i,char* word){
//gibt die ersten i zeichen des wortes zurück
}
/**
* Checks whether a word is in the Dictionary.
* Returns non-zero, if wird is in the dictionary and
* zero otherwise.
*/
int
Dictionary_isIn( const Dictionary* dict, const char* word );
/**
* Prints all words in the dictionary.
*/
void
Dictionary_print( const Dictionary* dict );
/**
* Adds all words from source to destination
*/
void
Dictionary_merge( Dictionary* destination, const Dictionary* source );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment