运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class SimpleHashMap: def __init__(self, size=100): self.size = size self.buckets = [[] for _ in range(size)] # A list of buckets, each is a list (to handle collisions) def hash_function(self, key): # Sum only the numerical values of the key, ignoring non-numeric characters numeric_sum = sum(int(char) for char in key if char.isdigit()) return numeric_sum % 10 # Perform modulo 10 on the sum def put(self, key, value): # Add or update a key-value pair index = self.hash_function(key) bucket = self.buckets[index] for i, (k, v) in enumerate(bucket): if k == key: bucket[i] = (key, value) # Update existing key return bucket.append((key, value)) # Add new key-value pair if not found def get(self, key): # Retrieve a value by key index = self.hash_function(key) bucket = self.buckets[index] for k, v in bucket: if k == key: return v return None # Key not found def remove(self, key): # Remove a key-value pair index = self.hash_function(key) bucket = self.buckets[index] for i, (k, v) in enumerate(bucket): if k == key: del bucket[i] # Remove the key-value pair return def print_map(self): # Print all key-value pairs in the hash map print("Hash Map Contents:") for index, bucket in enumerate(self.buckets): print(f"Bucket {index}: {bucket}") # Creating the Hash Map from the simulation hash_map = SimpleHashMap(size=10) # Adding some entries hash_map.put("123-4567", "Charlotte") hash_map.put("123-4568", "Thomas") hash_map.put("123-4569", "Jens") hash_map.put("123-4570", "Peter") hash_map.put("123-4571", "Lisa") hash_map.put("123-4672", "Adele") hash_map.put("123-4573", "Michaela") hash_map.put("123-6574", "Bob") hash_map.print_map() print("\nName associated with '123-4570':", hash_map.get("123-4570")) print("Updating the name for '123-4570' to 'James'") hash_map.put("123-4570","James") print("Name associated with '123-4570':", hash_map.get("123-4570")) #Python
#include <stdio.h> #include <stdlib.h> #include <string.h> #define BUCKET_COUNT 10 #define MAX_KEY_LEN 16 #define MAX_VAL_LEN 32 typedef struct Entry { char key[MAX_KEY_LEN]; char value[MAX_VAL_LEN]; struct Entry* next; } Entry; typedef struct { Entry* buckets[BUCKET_COUNT]; } SimpleHashMap; unsigned int hashFunction(const char* key) { unsigned int sum = 0; for (int i = 0; key[i] != '\0'; i++) { if (key[i] >= '0' && key[i] <= '9') { sum += key[i] - '0'; } } return sum % BUCKET_COUNT; } void initHashMap(SimpleHashMap* map) { for (int i = 0; i < BUCKET_COUNT; i++) { map->buckets[i] = NULL; } } void put(SimpleHashMap* map, const char* key, const char* value) { unsigned int index = hashFunction(key); Entry* current = map->buckets[index]; // Update value if key already exists while (current != NULL) { if (strcmp(current->key, key) == 0) { strncpy(current->value, value, MAX_VAL_LEN - 1); current->value[MAX_VAL_LEN - 1] = '\0'; return; } current = current->next; } // Add new key-value pair Entry* newEntry = (Entry*)malloc(sizeof(Entry)); strncpy(newEntry->key, key, MAX_KEY_LEN - 1); newEntry->key[MAX_KEY_LEN - 1] = '\0'; strncpy(newEntry->value, value, MAX_VAL_LEN - 1); newEntry->value[MAX_VAL_LEN - 1] = '\0'; newEntry->next = map->buckets[index]; map->buckets[index] = newEntry; } const char* get(SimpleHashMap* map, const char* key) { unsigned int index = hashFunction(key); Entry* current = map->buckets[index]; while (current != NULL) { if (strcmp(current->key, key) == 0) { return current->value; } current = current->next; } return NULL; // Key not found } void removeEntry(SimpleHashMap* map, const char* key) { unsigned int index = hashFunction(key); Entry* current = map->buckets[index]; Entry* prev = NULL; while (current != NULL) { if (strcmp(current->key, key) == 0) { if (prev != NULL) { prev->next = current->next; } else { map->buckets[index] = current->next; } free(current); return; } prev = current; current = current->next; } } void freeHashMap(SimpleHashMap* map) { for (int i = 0; i < BUCKET_COUNT; i++) { Entry* current = map->buckets[i]; while (current != NULL) { Entry* temp = current; current = current->next; free(temp); } } } void printHashMap(SimpleHashMap* map) { printf("Hash Map Contents:\n"); for (int i = 0; i < BUCKET_COUNT; i++) { Entry* current = map->buckets[i]; printf("Bucket %d: ", i); while (current != NULL) { printf("(%s: %s) ", current->key, current->value); current = current->next; } printf("\n"); } } int main() { SimpleHashMap map; initHashMap(&map); put(&map, "123-4567", "Charlotte"); put(&map, "123-4568", "Thomas"); put(&map, "123-4569", "Jens"); put(&map, "123-4570", "Peter"); put(&map, "123-4571", "Lisa"); put(&map, "123-4672", "Adele"); put(&map, "123-4573", "Michaela"); put(&map, "123-6574", "Bob"); printHashMap(&map); // Demonstrate get and put printf("\nName associated with '123-4570': %s\n", get(&map, "123-4570")); printf("Updating the name for '123-4570' to 'James'."); put(&map, "123-4570", "James"); printf("\nName associated with '123-4570': %s\n", get(&map, "123-4570")); freeHashMap(&map); return 0; } // C
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { SimpleHashMap hashMap = new SimpleHashMap(10); // Adding some entries hashMap.put("123-4567", "Charlotte"); hashMap.put("123-4568", "Thomas"); hashMap.put("123-4569", "Jens"); hashMap.put("123-4570", "Peter"); hashMap.put("123-4571", "Lisa"); hashMap.put("123-4672", "Adele"); hashMap.put("123-4573", "Michaela"); hashMap.put("123-6574", "Bob"); hashMap.printMap(); // Demonstrating retrieval and update System.out.println("\nName associated with '123-4570': " + hashMap.get("123-4570")); System.out.println("Updating the name for '123-4570' to 'James'"); hashMap.put("123-4570", "James"); System.out.println("Name associated with '123-4570': " + hashMap.get("123-4570")); } } class SimpleHashMap { private final List<List<Pair>> buckets; public SimpleHashMap(int size) { buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { buckets.add(new ArrayList<>()); } } private int hashFunction(String key) { int numericSum = key.chars() .filter(Character::isDigit) .map(Character::getNumericValue) .sum(); return numericSum % buckets.size(); } public void put(String key, String value) { int index = hashFunction(key); List<Pair> bucket = buckets.get(index); for (Pair pair : bucket) { if (pair.key.equals(key)) { pair.value = value; return; } } bucket.add(new Pair(key, value)); } public String get(String key) { int index = hashFunction(key); List<Pair> bucket = buckets.get(index); for (Pair pair : bucket) { if (pair.key.equals(key)) { return pair.value; } } return null; // Key not found } public void remove(String key) { int index = hashFunction(key); List<Pair> bucket = buckets.get(index); bucket.removeIf(pair -> pair.key.equals(key)); } public void printMap() { System.out.println("Hash Map Contents:"); for (int i = 0; i < buckets.size(); i++) { List<Pair> bucket = buckets.get(i); System.out.print("Bucket " + i + ": "); bucket.forEach(pair -> System.out.print("[" + pair.key + ": " + pair.value + "] ")); System.out.println(); } } } class Pair { String key; String value; public Pair(String key, String value) { this.key = key; this.value = value; } } //Java
Python 结果
C 结果
Java 结果
哈希映射内容
桶 0: [('123-4569', 'Jens')]
桶 1: []
桶 2: [('123-4570', 'Peter')]
桶 3: [('123-4571', 'Lisa')]
桶 4: []
桶 5: [('123-4672', 'Adele'), ('123-4573', 'Michaela')]
桶 6: []
桶 7: []
桶 8: [('123-4567', 'Charlotte'), ('123-6574', 'Bob')]
桶 9: [('123-4568', 'Thomas')]
与 '123-4570' 关联的名称:Peter
将 '123-4570' 的名称更新为 'James'
与 '123-4570' 关联的名称:James
哈希映射内容
桶 0: (123-4569: Jens)
桶 1
桶 2: (123-4570: Peter)
桶 3: (123-4571: Lisa)
桶 4
桶 5: (123-4573: Michaela) (123-4672: Adele)
桶 6
桶 7
桶 8: (123-6574: Bob) (123-4567: Charlotte)
桶 9: (123-4568: Thomas)
与 '123-4570' 关联的名称:Peter
将 '123-4570' 的名称更新为 'James'。
与 '123-4570' 关联的名称:James
哈希映射内容
桶 0: [123-4569: Jens]
桶 1
桶 2: [123-4570: Peter]
桶 3: [123-4571: Lisa]
桶 4
桶 5: [123-4672: Adele] [123-4573: Michaela]
桶 6
桶 7
桶 8: [123-4567: Charlotte] [123-6574: Bob]
桶 9: [123-4568: Thomas]
与 '123-4570' 关联的名称:Peter
将 '123-4570' 的名称更新为 'James'
与 '123-4570' 关联的名称:James