View difference between Paste ID: XVCrJ2s0 and M2TS2HP0
SHOW: | | - or go back to the newest paste.
1
/****************************************************************************
2
 * dictionary.c
3
 *
4
 * Computer Science 50
5
 * Problem Set 6
6
 *
7
 * Implements a dictionary's functionality.
8
 ***************************************************************************/
9
10
#include <stdbool.h>
11
#include <ctype.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <stdio.h>
15
16
#include "dictionary.h"
17
18
// my struct goes here:
19
typedef struct node
20
{
21
    char word[LENGTH + 1];
22
    struct node* next;
23
}
24
node;
25
// hashtable set to 26 creates 26 hashtable nodes, can we set every one of theese to point to NULL?
26
node* hashtable[26];// pointers in hashtable
27
node* new_word;
28
29
int key = 0; // to map into hashtable
30
long long int count = 0;// count loaded words
31
long long int pcount = 0; // pointer in size to return number of words 
32
33
/**
34
 * Returns true if word is in dictionary else false.
35
 */
36
bool check(const char* word) // word is the key
37
{
38-
    while(new_word->next != NULL)
38+
    while(new_word->next != NULL)//first check that new_word is not NULL
39
    {
40
        // make the case insensitive!!
41-
        if(isalpha(word[0]) && islower(word[0]))
41+
        if(isalpha(word[0]) && islower(word[0]))//you have the tolower() method provided by C use the man in the prompt more :)
42
        {
43
            key = word[0] - 'a';
44
        }
45
        if(isalpha(word[0]) && isupper(word[0]))
46
        {
47
            key = word[0] - 'A';
48
        }    
49-
        new_word = hashtable[key];
49+
        new_word = hashtable[key];//....not sure I get this: draw it on paper, your structure :)
50
        if(strcmp(word, new_word->word) == 0)
51
        {
52-
            return true;
52+
            return true;//ok
53
        }
54
            
55-
        if(new_word->next == NULL)
55+
        if(new_word->next == NULL)//you already have your while loop to check that , even if not ok.but how do you traverse your
56
//linked list? Draw again :)
57
        {
58
            return false;
59
        }
60
     }
61
     
62
    
63
    //
64
    return false;
65
}
66
67
/**
68
 * Loads dictionary into memory.  Returns true if successful else false.
69
 */
70
bool load(const char* dictionary)
71
{
72
    
73
    // open file
74
    FILE* fileptr = fopen(dictionary, "r");
75
    // if fileptr == NULL
76
    if(fileptr == NULL)
77
    {
78
        printf("Could not open\n");
79
        return false;
80
    }
81
    // while (fileptr != NULL)
82
    while(!feof(fileptr))
83
    {
84
    // create node new_word for word and a next pointer
85
        new_word = malloc(sizeof(node));
86
    
87
    // scan file for words and put into struct temp and in to member word
88
        fscanf(fileptr,"%s", new_word->word);
89
        count++;
90-
        if(isalpha(new_word->word[0]) && islower(new_word->word[0]))
90+
91
        if(isalpha(new_word->word[0]) && islower(new_word->word[0]))// no need. There is no trick in the dictionary: woeds are 
92
//lowercase and there are a to z and the apostrophe for some words
93
        {
94
            key = new_word->word[0] - 'a';
95
        }
96
        if(isalpha(new_word->word[0]) && isupper(new_word->word[0]))
97
        {
98
            key = new_word->word[0] - 'A';
99-
        new_word->next = hashtable[key];
99+
100
        // copy the address of struct head address to point to address of struct new_word next pointers address to head address
101
        new_word->next = hashtable[key];//you are misunderstanding inseartion by the head :)
102-
        hashtable[key] = new_word;
102+
103
        //head = new_word;
104
        hashtable[key] = new_word;//this is a better but not yet insertion by the head
105
        
106
    } 
107
    fclose(fileptr);
108
    return true;
109
}
110
111
/**
112
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
113
 */
114
unsigned int size(void)
115
{
116
    return pcount;
117
    
118
    //return 0;
119
}
120
121
/**
122
 * Unloads dictionary from memory.  Returns true if successful else false.
123
 */
124
bool unload(void)
125
{
126-
        free(hashtable[i]->next);
126+
127-
        free(new_word->word);
127+
128-
        free(hashtable[i]->word);
128+
        free(hashtable[i]->next);//while I have a word-->word-->word-->word-->NULL I free...you free only 1 node here
129
        free(new_word->word);//->word is part of the whole structure node, you free a node you free that part too
130
        free(hashtable[i]->word);//you didn't malloc it do not try to free what you didn't malloc
131
         
132
    }
133
    
134
    return false;
135
}