Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * dictionary.c
- *
- * Computer Science 50
- * Problem Set 6
- *
- * Implements a dictionary's functionality.
- ***************************************************************************/
- #include <stdbool.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "dictionary.h"
- // my struct goes here:
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node* next;
- }
- node;
- // hashtable set to 26 creates 26 hashtable nodes, can we set every one of theese to point to NULL?
- node* hashtable[26];// pointers in hashtable
- node* new_word;
- int key = 0; // to map into hashtable
- long long int count = 0;// count loaded words
- long long int pcount = 0; // pointer in size to return number of words
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word) // word is the key
- {
- while(new_word->next != NULL)//first check that new_word is not NULL
- {
- // make the case insensitive!!
- if(isalpha(word[0]) && islower(word[0]))//you have the tolower() method provided by C use the man in the prompt more :)
- {
- key = word[0] - 'a';
- }
- if(isalpha(word[0]) && isupper(word[0]))
- {
- key = word[0] - 'A';
- }
- new_word = hashtable[key];//....not sure I get this: draw it on paper, your structure :)
- if(strcmp(word, new_word->word) == 0)
- {
- return true;//ok
- }
- if(new_word->next == NULL)//you already have your while loop to check that , even if not ok.but how do you traverse your
- //linked list? Draw again :)
- {
- return false;
- }
- }
- //
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- // open file
- FILE* fileptr = fopen(dictionary, "r");
- // if fileptr == NULL
- if(fileptr == NULL)
- {
- printf("Could not open\n");
- return false;
- }
- // while (fileptr != NULL)
- while(!feof(fileptr))
- {
- // create node new_word for word and a next pointer
- new_word = malloc(sizeof(node));
- // scan file for words and put into struct temp and in to member word
- fscanf(fileptr,"%s", new_word->word);
- count++;
- // check the first letter which will be the key
- if(isalpha(new_word->word[0]) && islower(new_word->word[0]))// no need. There is no trick in the dictionary: woeds are
- //lowercase and there are a to z and the apostrophe for some words
- {
- key = new_word->word[0] - 'a';
- }
- if(isalpha(new_word->word[0]) && isupper(new_word->word[0]))
- {
- key = new_word->word[0] - 'A';
- }
- // copy the address of struct head address to point to address of struct new_word next pointers address to head address
- new_word->next = hashtable[key];//you are misunderstanding inseartion by the head :)
- // copy address of new_word to head to get the word there to be first
- //head = new_word;
- hashtable[key] = new_word;//this is a better but not yet insertion by the head
- }
- fclose(fileptr);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- return pcount;
- //return 0;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- for( int i = 0; i < 27; i++)
- {
- free(hashtable[i]->next);//while I have a word-->word-->word-->word-->NULL I free...you free only 1 node here
- free(new_word->word);//->word is part of the whole structure node, you free a node you free that part too
- free(hashtable[i]->word);//you didn't malloc it do not try to free what you didn't malloc
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement