Thread: This program is driving my crazy!

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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