ProgrammingHard
Which C code correctly inserts a node n at the front of bucket h of an array of struct node *table[]?
- A. table[h]->next = n;
- B. n->next = table[h]; table[h] = n;
- C. table[h] = n->next;
- D. table[h] = n; n->next = table[h];
Correct answer: B. n->next = table[h]; table[h] = n;
The new node must point to the old head before it becomes the head. The third version overwrites the head first and loses the chain.
More C Programming questions
- Which operation is needed to make open addressing deletions safe for later lookups?
- A program counts the distinct keys among 10^6 values by inserting them into a hash set with expected O(1) operations. What is the expected…
- Which property of the hash function matters most to avoid long chains when keys are multiples of 8 and the table has 8 buckets?
- A table of size 7 with h(k) = k % 7 is filled with 7 keys using linear probing and no deletions. How many more keys can be inserted before…
- Which search code is correct for an open-addressing table of size m with linear probing, where an empty slot ends the search?
- A hash table stores pointers to nodes allocated with malloc. What must happen when the table is destroyed?