Run ❯
Get your
own
website
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
#include
#include
#define SIZE 10 char table[SIZE][20]; // 10 buckets, each with room for a name int hash(char name[]) { int sum = 0; for (int i = 0; name[i] != '\0'; i++) { sum += name[i]; } return sum % SIZE; } void insert(char name[]) { int index = hash(name); // If the bucket is taken, try the next one while (table[index][0] != '\0') { index = (index + 1) % SIZE; } strcpy(table[index], name); } int find(char name[]) { int index = hash(name); while (table[index][0] != '\0') { if (strcmp(table[index], name) == 0) { return index; } index = (index + 1) % SIZE; } return -1; // An empty bucket: the name is not in the table } int main() { insert("Liam"); insert("Anja"); insert("Jenny"); insert("Ali"); printf("Ali is in bucket %d\n", find("Ali")); if (find("Tom") == -1) { printf("Tom is not in the table\n"); } return 0; }
Ali is in bucket 9
Tom is not in the table