Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * dictionary.c
- *
- * David J. Malan
- * malan@harvard.edu
- *
- * Implements a dictionary's functionality using a hash table.
- */
- #include <ctype.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include "dictionary.h"
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- // TODO
- //w = 0; //crawler!!!
- //Loading files:
- //node* new_node = malloc(sizeof(node));
- //node* head;
- //node* new_node;
- FILE* file = fopen(dictionary, "r");
- char *cur = NULL;
- fscanf(file, "%s", cur);
- int hashed;
- while(cur !=NULL) {
- hashed = hash(cur);
- node* head = table[hashed];
- node* new_node = malloc(sizeof(node));
- // new_node->next = head;
- // table[hashed] = new_node;
- //w++;
- if (head == NULL) {
- head = new_node;
- strcpy(new_node-> word, cur);
- table[hashed] = new_node;
- //w++;
- }
- //struct node *current = table[hashed];
- else{
- new_node->next = head;
- strcpy(new_node-> word, cur);
- table[hashed] = new_node;
- //w++;
- }
- // w++;
- fscanf(file, "%s", cur);
- }
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded. Else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- int n = 0;
- // printf("here!");
- for(int i = 0; i < BUCKETS+1; i++) {
- //printf("for loop: %d\n", i);
- if (table[i] == NULL) {
- printf("head null, i= %d, n = %d\n", i, n);
- }
- else {
- printf("head not null, i= %d\n", i);
- node* crawler = table[i];
- while(crawler->next != NULL){
- printf("here!");
- n++;
- crawler = crawler->next;
- // printf("here!");
- }
- //
- }
- }
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement