运行 ❯
获取您
的
网站
×
更改方向
更改主题,暗色/亮色
前往 Spaces
Python
C
Java
my_hash_set = [ [None], ['Jones'], [None], ['Lisa'], [None], ['Bob'], [None], ['Siri'], ['Pete'], [None] ] def hash_function(value): return sum(ord(char) for char in value) % 10 def add(value): index = hash_function(value) bucket = my_hash_set[index] if value not in bucket: bucket.append(value) def contains(value): index = hash_function(value) bucket = my_hash_set[index] return value in bucket add('Stuart') print(my_hash_set) print('Contains Stuart:',contains('Stuart')) #Python
#include <stdio.h> #include <stdlib.h> #include <string.h> #define HASH_SET_SIZE 10 typedef struct Node { char* value; struct Node* next; } Node; Node* myHashSet[HASH_SET_SIZE] = {NULL}; int hashFunction(const char* value) { int sum = 0; for (int i = 0; value[i] != '\0'; i++) { sum += value[i]; } return sum % HASH_SET_SIZE; } void add(const char* value) { int index = hashFunction(value); Node* newNode = (Node*)malloc(sizeof(Node)); // Manually allocate memory and copy the string newNode->value = (char*)malloc(strlen(value) + 1); // +1 for the null terminator strcpy(newNode->value, value); newNode->next = NULL; if (myHashSet[index] == NULL) { myHashSet[index] = newNode; } else { Node* current = myHashSet[index]; while (current->next != NULL) { current = current->next; } current->next = newNode; } } int contains(const char* value) { int index = hashFunction(value); Node* current = myHashSet[index]; while (current != NULL) { if (strcmp(current->value, value) == 0) { return 1; // True } current = current->next; } return 0; // False } void freeHashSet() { for (int i = 0; i < HASH_SET_SIZE; i++) { Node* current = myHashSet[i]; while (current != NULL) { Node* temp = current; current = current->next; free(temp->value); // Free the manually allocated string memory free(temp); } } } int main() { add("Jones"); add("Lisa"); add("Bob"); add("Siri"); add("Pete"); add("Stuart"); for (int i = 0; i < HASH_SET_SIZE; i++) { printf("Bucket %d: ", i); Node* current = myHashSet[i]; while (current != NULL) { printf("%s -> ", current->value); current = current->next; } printf("NULL\n"); } printf("\nContains Stuart: %s\n", contains("Stuart") ? "true" : "false"); freeHashSet(); // Cleanup allocated memory return 0; } // C
import java.util.LinkedList; public class Main { static LinkedList<String>[] myHashSet = new LinkedList[10]; public static void main(String[] args) { for (int i = 0; i < myHashSet.length; i++) { myHashSet[i] = new LinkedList<>(); } add("Jones"); add("Lisa"); add("Bob"); add("Siri"); add("Pete"); add("Stuart"); for (LinkedList<String> bucket : myHashSet) { System.out.println(bucket); } System.out.println("Contains Stuart: " + contains("Stuart")); } public static int hashFunction(String value) { int sum = 0; for (char c : value.toCharArray()) { sum += c; } return sum % 10; } public static void add(String value) { int index = hashFunction(value); LinkedList<String> bucket = myHashSet[index]; if (!bucket.contains(value)) { bucket.add(value); } } public static boolean contains(String value) { int index = hashFunction(value); LinkedList<String> bucket = myHashSet[index]; return bucket.contains(value); } } //Java
Python 结果
C 结果
Java 结果
[[None], ['Jones'], [None], ['Lisa', 'Stuart'], [None], ['Bob'], [None], ['Siri'], ['Pete'], [None]]
包含 Stuart: True
桶 0: NULL
桶 1: Jones -> NULL
桶 2: NULL
桶 3: Lisa -> Stuart -> NULL
桶 4: NULL
桶 5: Bob -> NULL
桶 6: NULL
桶 7: Siri -> NULL
桶 8: Pete -> NULL
桶 9: NULL
包含 Stuart: true
[]
[Jones]
[]
[Lisa, Stuart]
[]
[Bob]
[]
[Siri]
[Pete]
[]
包含 Stuart: true