Thread: This program is driving my crazy!

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    31

    This program is driving my crazy!

    Ok, so I need to use separate chaining hash in this program.

    This is my function that is suppose to add a new hash after the data has been added to the linked list.

    Code:
    typedef struct node {
        data list;
        struct node *next;
    } Node;
    
    typedef struct hashNode {
        Node *link;
        struct hashNode *next;
    } hNode;
    
    Node* hashtab[13];
    
    int add(char* name, char* number) {
        int i = 0;
        Node* n;
        if ((n = lookup(name)) == NULL) {
            i = hash(name);
            n = (Node*) malloc(sizeof(Node));
            if (n == NULL) return 0;
            strcpy(n->list.name, name);
            if (n->list.name == NULL) return 0;
            n->next = hashtab[i];
            hashtab[i] = n;
        }
        else
            free(n->list.number);
            strcpy(n->list.number, number);
            if(n->list.number == NULL) return 0;
    
      return 1;
    }
    
    Node* lookup(char *s) {
        int i = hash(s);
        Node *n = 0;
        n = hashtab[i]->link;
        for( ; n != NULL ; n = n->next) {
            if (!strcasecmp(n->list.name, s))
            return n;
        }
        return NULL;
    }
    What I'm doing wrong is in my add function, I create a Node, which contains the call
    data. I copy name and number into that Node, instead of using a DIFFERENT struct for
    the hash bucket list, because those nodes should POINT TO a linked list
    node, instead of containing the data.

    I think hashtab[13] should be hNode* instead right? IDK, I'm confused and don't know what to change.

    Thanks in advance!
    Last edited by zyphirr; 12-05-2008 at 04:12 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    My brain is tired. I don't want to think. What error are you getting?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not seeing enough code here to for sure say that I understand the problem... though I guess your main issue is that you are replacing blocks of data, correct? So just don't do that and problem solved.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    31
    I don't get errors, my program just doesn't do what I want it to.

    I don't know what you mean by changing blocks of data. I don't even know where to start changing.

    The program is too large to post.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Sooooooooooooooooooooooo, it is doing whatttttttttttttt, and you want it to do whatttttttttttttttt?
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Post anything that I may say "what does that do exactly?"

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    31
    So, I add name and data to a linked list. I need a function to find a specific name.

    Ex. find Smith

    And using the hash table, I look through to find if smith is one of the names I added.

    Currently, my find function doesn't find anything. Once again, I need separate chaining.

    Code:
    /* prints the first call that matches name */
    int find(char* name) {
        curr = lookup(name);
        if (curr == NULL) return 0;
        printf("%s %s\n", curr->list.name, curr->list.number);
        return 1;
    }
    
    /* string hash function */
    int hash(char *s) {
        int h = 0;
        int i;
        for (i = 0 ; s[i] != '\0'; i++) {
            s[i] = tolower((char) s[i]);
        }
        for( ; *s ; s++)
            h = *s + h*31;
        return h % 13;
    }
    
    /* insert hash data */
    int insert(char* name, char* number) {
        int i = 0;
        hNode *hNew = 0;
        if ((curr = lookup(name)) == NULL) {
            i = hash(name);
            hNew = (hNode*) malloc(sizeof(hNode));
            hNew->link = curr;
            if (hNew->link == NULL) return 0;
            strcpy(hNew->link->list.name, name);
            if (hNew->link->list.name == NULL) return 0;
            hNew->next = hashtab[i];
            hashtab[i] = hNew;
        }
        else
            free(curr->list.number);
            strcpy(curr->list.number, number);
            if(curr->list.number == NULL) return 0;
    
      return 1;
    }
    
    /* look up hash value */
    Node* lookup(char *s) {
        int i = hash(s);
        hNode *n = hashtab[i];
        for( ; n != NULL ; n = n->next) {
            strcpy(hashtab[i]->link->list.name, n->link->list.name);
            if (!strcasecmp(n->link->list.name, s)) {
                curr = n->link;
                return curr;
            }
        }
        return NULL;
    }

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you stepped through your code with an interactive debugger, did you see "smith" in the list of added items?
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your lookup function appears to destroy your list. It certainly shouldn't do any copying, just comparing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. IE 6 and Banner Ads is driving me crazy
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-09-2002, 02:18 AM
  4. This pop-up's is driving me crazy.
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 12-22-2001, 09:26 PM
  5. Replies: 0
    Last Post: 10-29-2001, 11:40 PM