How to write a C Program to Implements a dictionary's functionality in C Programming Language ?
Solution:
- /****************************************************************************
- * dictionary.c
* Implements a dictionary's functionality.
- ***************************************************************************/
- #include <stdbool.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "dictionary.h"
- // declare structure for each word
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node* next;
- }
- node;
- bool hash(const char* word)
- {
- // index < the number of buckets
- // deterministic - the same value needs to map to the same bucket every time
- }
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word)
- {
- // TODO
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- // TODO
- // declare file pointer for dictionary file
- FILE* input = NULL;
- // open dictionary
- input = fopen(dictionary, "r");
- if (input == NULL)
- {
- return false;
- }
- // declare memeory space or each word
- node* new_node = malloc(sizeof(node));
- while(input != EOF)
- {
- // scan dictionary after opening for each word before a\n and stores in in new_code->word
- fscanf(input, "%s", new_node->word);
- // hash the word (takes string and returns an index)
- hash(word);
- // this is a example on how to put a word in the front of the list
- new_node->next = head;
- head = new_node;
- }
- return false;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // TODO
- return 0;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- // TODO
- return false;
- }