Help! hash table

problem with searching. it has infinity loop when try to search..
and what should i use if i wan to change the user input to internal array, using strcmp?

anyone please help me~ thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
  #include <string.h>
  #include <stdlib.h>

  struct hash *hashTable = NULL;
  int eleCount = 0;

  struct node {
        int key, age;
        char name[100];
        struct node *next;
  };

  struct hash {
        struct node *head;
        int count;
  };

  struct node * createNode(int key, char *name, int age) {
        struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->key = key;
        newnode->age = age;
        strcpy(newnode->name, name);
        newnode->next = NULL;
        return newnode;
  }


  void insertToHash(int key, char *name, int age) {
        int hashIndex = key % eleCount;
        struct node *newnode =  createNode(key, name, age);
        /* head of list for the bucket with index "hashIndex" */
        if (!hashTable[hashIndex].head) {
                hashTable[hashIndex].head = newnode;
                hashTable[hashIndex].count = 1;
                return;
        }
        /* adding new node to the list */
        newnode->next = (hashTable[hashIndex].head);
        /*
         * update the head of the list and no of
         * nodes in the current bucket
         */
        hashTable[hashIndex].head = newnode;
        hashTable[hashIndex].count++;
        return;
  }


  void deleteFromHash(int key) {
        /* find the bucket using hash index */
        int hashIndex = key % eleCount, flag = 0;
        struct node *temp, *myNode;
        /* get the list head from current bucket */
        myNode = hashTable[hashIndex].head;
        if (!myNode) {
                printf("Given data is not present in hash Table!!\n");
                return;
        }
        temp = myNode;
        while (myNode != NULL) {
                /* delete the node with given key */
                if (myNode->key == key) {
                        flag = 1;
                        if (myNode == hashTable[hashIndex].head)
                                hashTable[hashIndex].head = myNode->next;
                        else
                                temp->next = myNode->next;

                        hashTable[hashIndex].count--;
                        free(myNode);
                        break;
                }
                temp = myNode;
                myNode = myNode->next;
        }
        if (flag)
                printf("Data deleted successfully from Hash Table\n");
        else
                printf("Given data is not present in hash Table!!!!\n");
        return;
  }

  void searchInHash(int key) {
        int hashIndex = key % eleCount, flag = 0;
        struct node *myNode;
        myNode = hashTable[hashIndex].head;
        if (!myNode) {
                printf("Search element unavailable in hash table\n");
                return;
        }
        while (myNode != NULL) {
                if (myNode->key == key) {
                        printf("VoterID  : %d\n", myNode->key);
                        printf("Name     : %s\n", myNode->name);
                        printf("Age      : %d\n", myNode->age);
                        flag = 1;
                        break;
                }
                myNode = myNode->next;
        }
        if (!flag)
                printf("Search element unavailable in hash table\n");
        return;
  }

  void display() {
        struct node *myNode;
        int i;
        for (i = 0; i < eleCount; i++) {
                if (hashTable[i].count == 0)
                        continue;
                myNode = hashTable[i].head;
                if (!myNode)
                        continue;
                printf("\nData at index %d in Hash Table:\n", i);
                printf("VoterID     Name          Age   \n");
                printf("--------------------------------\n");
                while (myNode != NULL) {
                        printf("%-12d", myNode->key);
                        printf("%-15s", myNode->name);
                        printf("%d\n", myNode->age);
                        myNode = myNode->next;
                }
        }
        return;
  }

  int main() {
        int n, ch, key, age;
        char name[100];
        printf("Enter the number of elements:");
        scanf("%d", &n);
        eleCount = n;
        /* create hash table with "n" no of buckets */
        hashTable = (struct hash *)calloc(n, sizeof (struct hash));
        while (1) {
                printf("\n1. Insertion\t2. Deletion\n");
                printf("3. Searching\t4. Display\n5. Exit\n");
                printf("Enter your choice:");
                scanf("%d", &ch);
                switch (ch) {
                        case 1:
                                printf("Enter the key value:");
                                scanf("%d", &key);
                                getchar();
                                printf("Name:");
                                fgets(name, 100, stdin);
                                name[strlen(name) - 1] = '\0';
                                printf("Age:");
                                scanf("%d", &age);
                                /*inserting new node to hash table */
                                insertToHash(key, name, age);
                                break;

                        case 2:
                                printf("Enter the key to perform deletion:");
                                scanf("%d", &key);
                                /* delete node with "key" from hash table */
                                deleteFromHash(key);
                                break;

                        case 3:
                                printf("Enter the key to search:");
                                scanf("%d", &key);
                                searchInHash(key);
                                break;
                        case 4:
                                display();
                                break;
                        case 5:
                                exit(0);
                        default:
				printf("U have entered wrong option!!\n");
                                break;
					 }
		  }  

  }
Last edited on
problem with searching. it has infinity loop when try to search..

Perhaps you can supply the input sequence that causes the infinite loop? I don't see anything obvious.
Last edited on
that one solved dy.. but if i wan to hav information that exist inside the program such as
1. key=sam, value = paul
2. key=kit, value = ivan
3. key=web, value=spider

how can i put the informations for key1 and value1 linking together in the hash table?

i cant find the example anywhere..
Last edited on
I'm assuming that you mean each node has a key and a value. I'm also assuming that you want to look up the nodes based on the key and do it using a hash table.

So your node should look something like this:
1
2
3
4
5
6
7
struct node {
        int key, age;
        char name[100];
        char keyword[20];
        char value[30];
        struct node *next;
  };

BTW, you probably should use string instead of a char array since string can hold any number of characters.

Okay, now the part that you're missing is the hash function. This takes the keyword (the string that you want to look things up with) and creates a number that you'll use as the hash key:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct node {
        int hash() const;
        ...
};

int node::hash() const
{
    int result = 0;
    for (const char *cp = keywd; *cp; ++cp) {
        result += *cp;
    }
    return result;
}


Important note! The example hash function isn't very good. For example, it will produce the same hash value for keywords "cat" and "tac". Choosing a good hash function is critical to the performance of the hash table.

Change createNode to take the keyword and value. It should compute the hash key. Better still, make this a constructor in struct node:
1
2
3
4
5
6
7
8
node::node(const char *namep, int agep, const char *keyp, const char *valp)
{
    strcpy(name, namep);
    age = agep;
    strcpy(keywd, keyp);
    strcpy(value, valp);
    key = hash();
}

Replace InsertToHash() with a function that takes a reference to a node. Let the caller create the node:
1
2
3
4
5
6
7
8
9
10
11
InsertToHash(node &newnode)
{
        int hashIndex =newnode. key % eleCount;
        /* head of list for the bucket with index "hashIndex" */
        if (!hashTable[hashIndex].head) {
                hashTable[hashIndex].head = &newnode;
                hashTable[hashIndex].count = 1;
                return;
        }
        // etc
}


Add search and delete methods that take a keyword. These can call your existing functions. For example:
1
2
3
4
5
void searchInHash(const char *k)
{
    node dummy("", 0, k, "");
    searchInHash(dummy.key);
}


As a final comment, I'll point out that this would be better if there were a HashTable class with insert/delete/search methods. The table array would be private data.
Topic archived. No new replies allowed.