hash stuff... what next?

This is a discussion on hash stuff... what next? within the C Programming forums, part of the General Programming Boards category; I am reading from a file 3 things, name, street, and address. And I want to put it in a ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    hash stuff... what next?

    I am reading from a file 3 things, name, street, and address. And I want to put it in a hash table with 6 buckets for easy access. I searched around and got some code which started me out. but now i have no idea how to initilize and add 60 people into it. I understand that I must use some algorithim to turn the name into a key, but what do i do when i get the key?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STRINGBUF 1000
    #define BUCKETNUM 6
    
    
          typedef struct tperson{
          char name[STRINGBUF];
          char address[STRINGBUF];
          int key;
          struct tperson *next;
       }person;
       
       //function declarations
       void ReadList(void);
       void InitHash(void);
       void HashFunction(void);
       void PrintHashTable(void);
    
        int main(){
          return 0;
       }
    
        void InitHash(){
       }
    
        void HashFunction(){
       }
    
        void PrintHashTable(){
       }
    
        void ReadList(){
          FILE *filelist;
          filelist=fopen("fileit.txt", "r");
          char line[STRINGBUF];
          char *name,*street,*city;
       
          while(fgets(line, sizeof(line), filelist)!=NULL){
             name=strtok(line,";");
             street=strtok(NULL,";");
             city=strtok(NULL,";");
          }
          fclose(filelist);
       }
    Last edited by Butters007; 12-05-2006 at 09:12 AM.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    For every person, your hashing function will get you a key. When you get that key, you want to insert that person at the place in your hash table indicated by that key. Simple.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    i just dont want a large linked list.... where do the 6 buckets come into play? thats the thing, i dont know how to insert it. say i get "14" as a key. how do i insert a the name/address/stuff in the appropiate bucket at the appropiate spot.
    Last edited by Butters007; 12-05-2006 at 08:56 AM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    i just dont want a large linked list

    then your going to have to make up a good hash function, that will distribute the keys good

    with only 6 buckets, I think the best way is to use the key to index directly into the table

    ex. table[key] = element;

    this assumes your hash function is setup accordingly, so 0 <= key < 6
    Last edited by sl4nted; 12-05-2006 at 09:33 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do i insert a the name/address/stuff in the appropiate bucket at the appropiate spot.
    http://eternallyconfuzzled.com/tuts/....html#chaining
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicate at Hash Table
    By DarrenY in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 02:31 AM
  2. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  3. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  4. Hash and more hash
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-24-2002, 07:12 AM
  5. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21