Skip to content

Instantly share code, notes, and snippets.

Created March 13, 2016 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ca9c95f07e436289dd3a to your computer and use it in GitHub Desktop.
Save anonymous/ca9c95f07e436289dd3a to your computer and use it in GitHub Desktop.
Issue with seg faults :(
/**
* dictionary.c
*
* Computer Science 50
* Problem Set 5
*
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
#define TABLESIZE 100
unsigned int hash(char* word);
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
node* hashtable[TABLESIZE];
/**
* 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.
*/
int wordcount;
bool load(const char* dictionary)
{
// open the file
FILE* file = fopen(dictionary, "r");
// check for errors opening the file
if (file == NULL)
{
printf("Error opening the file %s\n", dictionary);
return false;
}
// create a buffer to keep the current string on
char buffer[LENGTH + 1];
// Fill the array with null pointers, NULL means free space and also end of list
for(int i = 0; i < TABLESIZE; i++)
{
hashtable[i] = NULL;
}
// iterate over every string until end of file, and store it at buffer
for(int i = fscanf(file, "%s", buffer); i != EOF; fscanf(file, "%s", buffer))
{
// the index to check if used or not is the hash of the word which has been stored in buffer
int index = hash(buffer);
// overwrite \n with '\0'
if(buffer[strlen(buffer)] == '\n')
{
buffer[strlen(buffer)] = '\0';
}
// check for null aka check if first element to be added to the list in said index
if (hashtable[index] == NULL)
{
strcpy(hashtable[index] -> word , buffer);
hashtable[index] -> next = NULL;
free(buffer);
wordcount++;
}
// if not first element, append to the head
else
{
// create a temporal node in which we'll move the actual head
node* new_node = malloc(sizeof(node));
// check for errors
if (new_node == NULL)
{
printf("Couldn't add word %s", buffer);
fclose(file);
free(buffer);
return false;
}
// store the string in a new node
strcpy(new_node -> word, hashtable[i] -> word);
// the head will point to this new node
hashtable[index] -> next = new_node;
// and the head will have the new string
strcpy(hashtable[index] -> word, buffer);
free(buffer);
wordcount++;
}
}
fclose(file);
return true;
}
unsigned int hash(char* word)
{
int hash = 0;
for(int i = 0, strlength = strlen(word); i < strlength; i++)
{
hash += word[i];
}
return hash;
}
/**
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment