c
复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// 定义哈希表的节点结构体
typedef struct HashNode
{
char *key;
void *value;
struct HashNode *next;
} HashNode;
// 定义哈希表结构体
typedef struct HashTable
{
HashNode **table;
size_t size;
} HashTable;
// 创建哈希表
HashTable *create_hash_table(size_t size)
{
HashTable *hash_table = malloc(sizeof(HashTable));
hash_table->table = malloc(sizeof(HashNode *) * size);
hash_table->size = size;
memset(hash_table->table, 0, sizeof(HashNode *) * size);
return hash_table;
}
// 销毁哈希表
void destroy_hash_table(HashTable *hash_table)
{
size_t i;
for (i = 0; i < hash_table->size; ++i)
{
HashNode *node = hash_table->table[i];
while (node)
{
HashNode *next = node->next;
free(node->key);
free(node->value);
free(node);
node = next;
}
}
free(hash_table->table);
free(hash_table);
}
// 计算哈希值
size_t hash(const char *key, size_t size)
{
size_t hash = 5381;
int c;
while ((c = *key++))
{
hash = ((hash << 5) + hash) + c;
}
return hash % size;
}
// 在哈希表中插入键值对
bool insert_hash_table(HashTable *hash_table, const char *key, void *value)
{
size_t index = hash(key, hash_table->size);
HashNode *new_node = malloc(sizeof(HashNode));
new_node->key = strdup(key);
new_node->value = value;
new_node->next = hash_table->table[index];
hash_table->table[index] = new_node;
return true;
}
// 从哈希表中获取值
void *get_from_hash_table(HashTable *hash_table, const char *key)
{
size_t index = hash(key, hash_table->size);
HashNode *node = hash_table->table[index];
while (node)
{
if (strcmp(node->key, key) == 0)
{
return node->value;
}
node = node->next;
}
return NULL;
}
// 用户结构体
typedef struct User
{
char *name;
} User;
// 网站结构体
typedef struct Website
{
char *name;
} Website;
// 网站工厂结构体
typedef struct WebsiteFactory
{
HashTable *flyweights;
} WebsiteFactory;
// 创建用户
User *create_user(const char *name)
{
User *user = malloc(sizeof(User));
user->name = strdup(name);
return user;
}
// 销毁用户
void destroy_user(User *user)
{
free(user->name);
free(user);
}
// 创建网站
Website *create_website(const char *name)
{
Website *website = malloc(sizeof(Website));
website->name = strdup(name);
return website;
}
// 销毁网站
void destroy_website(Website *website)
{
free(website->name);
free(website);
}
// 使用网站
void use_website(Website *website, User *user)
{
printf("网站分类:%s 用户:%s\n", website->name, user->name);
}
// 创建网站工厂
WebsiteFactory *create_website_factory()
{
WebsiteFactory *factory = malloc(sizeof(WebsiteFactory));
factory->flyweights = create_hash_table(10);
return factory;
}
// 销毁网站工厂
void destroy_website_factory(WebsiteFactory *factory)
{
destroy_hash_table(factory->flyweights);
free(factory);
}
// 从工厂获取网站
Website *get_website_category(WebsiteFactory *factory, const char *key)
{
Website *website = get_from_hash_table(factory->flyweights, key);
if (!website)
{
website = create_website(key);
insert_hash_table(factory->flyweights, key, website);
}
return website;
}
int main()
{
WebsiteFactory *f = create_website_factory();
Website *fx = get_website_category(f, "产品展示");
User *user_fx = create_user("小菜");
use_website(fx, user_fx);
destroy_user(user_fx);
Website *fy = get_website_category(f, "产品展示");
User *user_fy = create_user("大鸟");
use_website(fy, user_fy);
destroy_user(user_fy);
Website *fz = get_website_category(f, "产品展示");
User *user_fz = create_user("娇娇");
use_website(fz, user_fz);
destroy_user(user_fz);
Website *fl = get_website_category(f, "博客");
User *user_fl = create_user("老顽童");
use_website(fl, user_fl);
destroy_user(user_fl);
Website *fm = get_website_category(f, "博客");
User *user_fm = create_user("桃谷六仙");
use_website(fm, user_fm);
destroy_user(user_fm);
Website *fn = get_website_category(f, "博客");
User *user_fn = create_user("南海鳄神");
use_website(fn, user_fn);
destroy_user(user_fn);
size_t count = 0;
size_t i;
for (i = 0; i < f->flyweights->size; ++i)
{
if (f->flyweights->table[i])
{
++count;
}
}
printf("得到网站分类总数:%zu\n", count);
destroy_website_factory(f);
return 0;
}