#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 1000
typedef struct {
char *voter_id;
} Vote;
Vote *hash_table[TABLE_SIZE];
unsigned int hash(char *id) {
unsigned long hash = 0;
unsigned int c;
while (c = (unsigned int)*id++) {
hash = hash * 37 + c;
}
return hash % TABLE_SIZE;
}
int insert(char *voter_id) {
unsigned int index = hash(voter_id);
if (hash_table[index] != NULL) {
return 0;
}
hash_table[index] = malloc(sizeof(Vote));
hash_table[index]->voter_id = strdup(voter_id);
return 1;
}
int main() {
char *voters[] = {"Alice", "Bob", "Charlie", "David", NULL};
char **current_voter = voters;
while (*current_voter != NULL) {
if (insert(*current_voter)) {
printf("%s has voted successfully.\n", *current_voter);
} else {
printf("%s is trying to vote again!\n", *current_voter);
}
current_voter++;
}
for (int i = 0; i < TABLE_SIZE; i++) {
if (hash_table[i] != NULL) {
free(hash_table[i]->voter_id);
free(hash_table[i]);
}
}
return 0;
}