C programming problem

I have this code:


/* 
 * File:   main.c
 * Author: Danny
 *
 * Created on November 26, 2009, 11:59 PM
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
 
/*node of the list which will store the words while "hanging" of the array*/
typedef struct ListNode {
    char* word;
    int size;
    struct List *next;
} List;
 
typedef struct {
    List** list;
    int size;
} HashTable;
 
/*returns a initialized hash table*/
HashTable* createTable(int tableSize) {
 
    int i = 0; /*loop iterator*/
    HashTable* newTable = (HashTable*) malloc(sizeof (HashTable)); /*new hashtable pointer to be returned*/
    newTable->list = (List**) malloc(sizeof (List*) * tableSize); /*malloc for the hashTable*/
    newTable->size = tableSize;
 
    /**
 
    for (i = 0; i < tableSize; i++) {
 
        newList[i].next = NULL;
        newList[i].size = 0;
        newList[i].word = NULL;
    }
     **/
 
    /*assignment of the new hashTable's list to the new list created*/
    //newTable->list = &newList;
 
    return newTable; /*new HashTable returned*/
}
 
/*returns 1 if the hash table list at location "key" is empty*/
int HashKeyEmpty(HashTable* H, int key) {
    if (H->list[key] == NULL) return 1;
    return 0;
}
 
/*inserts a node to a list*/
void ListInsert(HashTable* H, int key, List* node) {
 
    List* listPtr = H->list[key];
 
 
 
    if (listPtr == NULL)
        H->list[key] = node;
    else {
        // traverse to end
        while (listPtr->next != NULL)
            listPtr = listPtr->next;
        listPtr->next = node;
    }
 
 
}
 
/*prints the whole list nodes*/
void PrintList(List* list) {
    List* listPtr = NULL;
    listPtr = &list;
 
    while(listPtr != NULL)
    {
        printf("%s ,",listPtr->word);
        listPtr = listPtr->next;
    }
  if(list == NULL)
      printf("Empty\n");
}
 
void HashInsert(HashTable* H, char *word) {
    int key = 0;
    int i = 0;
    List* tempNode;
 
    for (i = 0; i < strlen(word); i++)
        key = (key * 31 + word[i]);
 
    key = labs(key) % H->size;
 
    List* newNode;
    newNode = (List*) malloc(sizeof (List));
    newNode->next = NULL;
    newNode->word = (char*) malloc(strlen(word) + 1);
    strcpy(newNode->word, word);
    ListInsert(H, key, newNode);
}
 
int main(int argc, char** argv) {
 
    FILE *fp;
    char *word;
    double number_of_words = 0;
    double hashSize = 0;
    int tableSize = 0;
    int h;
    int i;
 
    fp = fopen("ispell.wordlist", "r");
 
    //count the number of words
    while (fscanf(fp, "%s", word) == 1) {
        number_of_words++;
    }
 
    fclose(fp);
    //approximately 20%
    hashSize = (ceil(number_of_words * 0.01)* 100 + 100) * 0.2;
    tableSize = (int) hashSize;
    printf("%d \n", tableSize);
 
    HashTable* hashTable = NULL;
    hashTable = createTable(tableSize);
 
    fp = fopen("ispell.wordlist", "r");
 
    i=0;
    while (fscanf(fp, "%s", word) == 1) {
        HashInsert(hashTable, word);
        i++;
    }
    fprintf("%s \n", hashTable->list[100]->word); <------ the problem
 
    return (EXIT_SUCCESS);
}

 

how do I access this word that inside my hashtable?!


Share Send to a friend Watch Report
 
 

Posted Answers

No answers were posted yet - be the first one to answer!

Sign in to participate

Got an answer for dannyvas? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


Q:

Web designer vs graphics designer

What is the difference between web designer and graphic designer???
Submitted by mint1   4 months ago.
  • viewed 669 times
Last answer posted 2 months ago by mint1


Q:

Can I get my logo designed strictly through the ...

Can I get my logo designed strictly through the Web? Luke
Submitted by Luke Southwell   2 months ago.
  • viewed 329 times
Last answer posted 1 month ago by MayaLocke



» More...

Explore Related Posts in Forums

Sharing Files between NT 4.0 Computers and Win95 Computers

computers to Win95 computers but not vice versa. I can create the shares on the WIn95 computers Are the Win95 computers backing up their data regularly? If not then you have a case for everything computer when I'm on a NT computer. I can even gain access from the Win95 computers when I'm on the...

ClickOnce Deployment Fails on SOME computers, but not ALL computers....

Publish" in the VB.Net IDE. On a majority of computers (All are Windows XP SP3 with .Net 2.0, .Net 3.0 , and .Net 3.5 SP1 loaded), it runs fine. On a minority of computers, when I click the Launch button tell us. Hi Stuart, > On a majority of computers (All are Windows XP SP3 with .Net 2.0, .Net 3.0

[News] Computers & Technology News [Post Your NEWS Here] - Computers...

Computers & Technology News Thread - Post All Your NEWS Articles In This Thread - - Good Articles -day event drew about 160 developers and other interested parties to the Computer History Museum is an Audio & MIDI Digital Multitrack Recorder that transforms your computer into a powerful audio
» More...
Powered by
Feed - Subscribe to changes to this Q&A Blog
ADVERTISEMENT
  • Answers
  • Web
Copyright © 2006-2010, Yedda Inc. and respective copyright owners · CC License