#define SIZE 100000
// 1. hash_function() is a function that return an int
// 2. snowflake_node
typedef struct snowflake_node {
int snowflake[6];
struct snowflake_node *next;
} snowflake_node;
// 3. main
int main(void) {
static snowflake_node *snowflakes[SIZE] = {NULL};
snowflake_node *snow;
int n, i, j, hash_code;
scanf(“%d”, &n);
for (i = 0; i < n; i++) {
snow = malloc(sizeof(snowflake_node));
if (snow == NULL) {
fprintf(stderr, ”malloc errorn”);
exit(1);
}
for (j = 0; j < 6; j++)
scanf(“%d”, &snow->snowflake[j]);
hash_code = hash_function(snow->snowflake);
snow->next = snowflakes[hash_code];
// 4. This confuses me
snowflakes[hash_code] = snow;
}
return 0;
}
So, I am still trying to grasp how I can implement a Linked List in C. I quite understand that each nodes in a Linked List has a pointer to the next.
What confuses me is that number 4 part. Let’s say that the first Snow
has the hash_code
of 21. This is going to be pointer to it.
It turns out that the second Snow
also has the hash_code
of 21. If we apply it to the code :
snowflakes[hash_code] = snow
Does that mean we replace the current Snow
with the new one, instead of chaining them all together?
arifnone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.