c语言中如何创建字典

c语言中如何创建字典

在C语言中创建字典的几种方法包括:使用结构体、使用哈希表、使用平衡二叉树。本文将详细介绍这些方法中的一种——使用哈希表来创建字典。

一、使用结构体创建字典

1、结构体介绍

在C语言中,结构体(struct)是一种用户自定义的数据类型,它允许将不同类型的数据组合在一起。我们可以使用结构体来创建一个简单的字典,其中包含键和值。

2、定义结构体

首先,我们需要定义一个结构体来表示字典中的每个项。这个结构体将包含一个键和一个值:

#include <stdio.h>

#include <string.h>

#define MAX_KEY_LENGTH 50

#define MAX_VALUE_LENGTH 50

struct DictionaryItem {

char key[MAX_KEY_LENGTH];

char value[MAX_VALUE_LENGTH];

};

在这个定义中,我们使用字符数组来存储键和值。MAX_KEY_LENGTHMAX_VALUE_LENGTH是键和值的最大长度。

3、创建字典

接下来,我们需要一个数组来存储这些结构体项。我们还需要一个变量来跟踪字典中当前的项数。

#define DICTIONARY_SIZE 100

struct DictionaryItem dictionary[DICTIONARY_SIZE];

int dictionarySize = 0;

4、添加项

我们可以编写一个函数来向字典中添加新项:

void addDictionaryItem(const char* key, const char* value) {

if (dictionarySize < DICTIONARY_SIZE) {

strcpy(dictionary[dictionarySize].key, key);

strcpy(dictionary[dictionarySize].value, value);

dictionarySize++;

} else {

printf("Dictionary is full.n");

}

}

5、查找项

我们还需要一个函数来根据键查找值:

const char* getDictionaryValue(const char* key) {

for (int i = 0; i < dictionarySize; i++) {

if (strcmp(dictionary[i].key, key) == 0) {

return dictionary[i].value;

}

}

return NULL;

}

6、示例代码

下面是一个完整的示例代码,演示如何使用上述函数:

#include <stdio.h>

#include <string.h>

#define MAX_KEY_LENGTH 50

#define MAX_VALUE_LENGTH 50

#define DICTIONARY_SIZE 100

struct DictionaryItem {

char key[MAX_KEY_LENGTH];

char value[MAX_VALUE_LENGTH];

};

struct DictionaryItem dictionary[DICTIONARY_SIZE];

int dictionarySize = 0;

void addDictionaryItem(const char* key, const char* value) {

if (dictionarySize < DICTIONARY_SIZE) {

strcpy(dictionary[dictionarySize].key, key);

strcpy(dictionary[dictionarySize].value, value);

dictionarySize++;

} else {

printf("Dictionary is full.n");

}

}

const char* getDictionaryValue(const char* key) {

for (int i = 0; i < dictionarySize; i++) {

if (strcmp(dictionary[i].key, key) == 0) {

return dictionary[i].value;

}

}

return NULL;

}

int main() {

addDictionaryItem("name", "Alice");

addDictionaryItem("age", "30");

const char* name = getDictionaryValue("name");

const char* age = getDictionaryValue("age");

if (name != NULL) {

printf("name: %sn", name);

} else {

printf("Key 'name' not found.n");

}

if (age != NULL) {

printf("age: %sn", age);

} else {

printf("Key 'age' not found.n");

}

return 0;

}

这个示例代码展示了如何使用结构体和数组来创建一个简单的字典,并向其中添加项和查找值。

二、使用哈希表创建字典

1、哈希表介绍

哈希表是一种数据结构,它使用键来存储和检索值。哈希表通过哈希函数将键映射到数组中的索引位置,从而实现高效的查找操作。在C语言中,我们可以使用数组和链表来实现哈希表。

2、定义哈希表项

首先,我们需要定义一个结构体来表示哈希表中的每个项。这个结构体将包含一个键、一个值和一个指向下一个项的指针(用于处理哈希冲突):

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_KEY_LENGTH 50

#define MAX_VALUE_LENGTH 50

#define HASH_TABLE_SIZE 100

struct HashTableItem {

char key[MAX_KEY_LENGTH];

char value[MAX_VALUE_LENGTH];

struct HashTableItem* next;

};

3、定义哈希表

接下来,我们需要一个数组来存储这些哈希表项。这个数组的大小是哈希表的大小:

struct HashTableItem* hashTable[HASH_TABLE_SIZE];

4、哈希函数

我们需要一个哈希函数来将键映射到数组中的索引位置。这个哈希函数应该返回一个介于0和HASH_TABLE_SIZE-1之间的整数:

unsigned int hash(const char* key) {

unsigned int hashValue = 0;

while (*key) {

hashValue = (hashValue << 5) + *key++;

}

return hashValue % HASH_TABLE_SIZE;

}

5、添加项

我们可以编写一个函数来向哈希表中添加新项:

void addHashTableItem(const char* key, const char* value) {

unsigned int index = hash(key);

struct HashTableItem* newItem = (struct HashTableItem*)malloc(sizeof(struct HashTableItem));

strcpy(newItem->key, key);

strcpy(newItem->value, value);

newItem->next = hashTable[index];

hashTable[index] = newItem;

}

6、查找项

我们还需要一个函数来根据键查找值:

const char* getHashTableValue(const char* key) {

unsigned int index = hash(key);

struct HashTableItem* item = hashTable[index];

while (item != NULL) {

if (strcmp(item->key, key) == 0) {

return item->value;

}

item = item->next;

}

return NULL;

}

7、示例代码

下面是一个完整的示例代码,演示如何使用上述函数:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_KEY_LENGTH 50

#define MAX_VALUE_LENGTH 50

#define HASH_TABLE_SIZE 100

struct HashTableItem {

char key[MAX_KEY_LENGTH];

char value[MAX_VALUE_LENGTH];

struct HashTableItem* next;

};

struct HashTableItem* hashTable[HASH_TABLE_SIZE];

unsigned int hash(const char* key) {

unsigned int hashValue = 0;

while (*key) {

hashValue = (hashValue << 5) + *key++;

}

return hashValue % HASH_TABLE_SIZE;

}

void addHashTableItem(const char* key, const char* value) {

unsigned int index = hash(key);

struct HashTableItem* newItem = (struct HashTableItem*)malloc(sizeof(struct HashTableItem));

strcpy(newItem->key, key);

strcpy(newItem->value, value);

newItem->next = hashTable[index];

hashTable[index] = newItem;

}

const char* getHashTableValue(const char* key) {

unsigned int index = hash(key);

struct HashTableItem* item = hashTable[index];

while (item != NULL) {

if (strcmp(item->key, key) == 0) {

return item->value;

}

item = item->next;

}

return NULL;

}

int main() {

addHashTableItem("name", "Alice");

addHashTableItem("age", "30");

const char* name = getHashTableValue("name");

const char* age = getHashTableValue("age");

if (name != NULL) {

printf("name: %sn", name);

} else {

printf("Key 'name' not found.n");

}

if (age != NULL) {

printf("age: %sn", age);

} else {

printf("Key 'age' not found.n");

}

return 0;

}

这个示例代码展示了如何使用数组和链表来实现一个哈希表,并向其中添加项和查找值。

三、使用平衡二叉树创建字典

1、平衡二叉树介绍

平衡二叉树是一种二叉树数据结构,其中每个节点都有一个键和一个值。树的左右子树高度差不超过1,从而保证查找、插入和删除操作的时间复杂度为O(log n)。

2、定义树节点

首先,我们需要定义一个结构体来表示树中的每个节点。这个结构体将包含一个键、一个值、左子节点和右子节点:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_KEY_LENGTH 50

#define MAX_VALUE_LENGTH 50

struct TreeNode {

char key[MAX_KEY_LENGTH];

char value[MAX_VALUE_LENGTH];

struct TreeNode* left;

struct TreeNode* right;

int height;

};

3、创建新节点

我们可以编写一个函数来创建一个新节点:

struct TreeNode* createTreeNode(const char* key, const char* value) {

struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));

strcpy(newNode->key, key);

strcpy(newNode->value, value);

newNode->left = NULL;

newNode->right = NULL;

newNode->height = 1;

return newNode;

}

4、获取节点高度

我们需要一个函数来获取节点的高度:

int getHeight(struct TreeNode* node) {

if (node == NULL) {

return 0;

}

return node->height;

}

5、更新节点高度

我们还需要一个函数来更新节点的高度:

void updateHeight(struct TreeNode* node) {

if (node != NULL) {

node->height = 1 + (getHeight(node->left) > getHeight(node->right) ? getHeight(node->left) : getHeight(node->right));

}

}

6、旋转操作

为了保持树的平衡,我们需要实现左右旋转操作:

struct TreeNode* rotateRight(struct TreeNode* y) {

struct TreeNode* x = y->left;

struct TreeNode* T2 = x->right;

x->right = y;

y->left = T2;

updateHeight(y);

updateHeight(x);

return x;

}

struct TreeNode* rotateLeft(struct TreeNode* x) {

struct TreeNode* y = x->right;

struct TreeNode* T2 = y->left;

y->left = x;

x->right = T2;

updateHeight(x);

updateHeight(y);

return y;

}

7、平衡因子

我们需要一个函数来计算节点的平衡因子:

int getBalance(struct TreeNode* node) {

if (node == NULL) {

return 0;

}

return getHeight(node->left) - getHeight(node->right);

}

8、插入节点

我们可以编写一个函数来向树中插入新节点,并保持树的平衡:

struct TreeNode* insertTreeNode(struct TreeNode* node, const char* key, const char* value) {

if (node == NULL) {

return createTreeNode(key, value);

}

if (strcmp(key, node->key) < 0) {

node->left = insertTreeNode(node->left, key, value);

} else if (strcmp(key, node->key) > 0) {

node->right = insertTreeNode(node->right, key, value);

} else {

strcpy(node->value, value);

return node;

}

updateHeight(node);

int balance = getBalance(node);

if (balance > 1 && strcmp(key, node->left->key) < 0) {

return rotateRight(node);

}

if (balance < -1 && strcmp(key, node->right->key) > 0) {

return rotateLeft(node);

}

if (balance > 1 && strcmp(key, node->left->key) > 0) {

node->left = rotateLeft(node->left);

return rotateRight(node);

}

if (balance < -1 && strcmp(key, node->right->key) < 0) {

node->right = rotateRight(node->right);

return rotateLeft(node);

}

return node;

}

9、查找节点

我们还需要一个函数来根据键查找值:

const char* getTreeNodeValue(struct TreeNode* node, const char* key) {

if (node == NULL) {

return NULL;

}

if (strcmp(key, node->key) < 0) {

return getTreeNodeValue(node->left, key);

} else if (strcmp(key, node->key) > 0) {

return getTreeNodeValue(node->right, key);

} else {

return node->value;

}

}

10、示例代码

下面是一个完整的示例代码,演示如何使用上述函数:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_KEY_LENGTH 50

#define MAX_VALUE_LENGTH 50

struct TreeNode {

char key[MAX_KEY_LENGTH];

char value[MAX_VALUE_LENGTH];

struct TreeNode* left;

struct TreeNode* right;

int height;

};

struct TreeNode* createTreeNode(const char* key, const char* value) {

struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));

strcpy(newNode->key, key);

strcpy(newNode->value, value);

newNode->left = NULL;

newNode->right = NULL;

newNode->height = 1;

return newNode;

}

int getHeight(struct TreeNode* node) {

if (node == NULL) {

return 0;

}

return node->height;

}

void updateHeight(struct TreeNode* node) {

if (node != NULL) {

node->height = 1 + (getHeight(node->left) > getHeight(node->right) ? getHeight(node->left) : getHeight(node->right));

}

}

struct TreeNode* rotateRight(struct TreeNode* y) {

struct TreeNode* x = y->left;

struct TreeNode* T2 = x->right;

x->right = y;

y->left = T2;

updateHeight(y);

updateHeight(x);

return x;

}

struct TreeNode* rotateLeft(struct TreeNode* x) {

struct TreeNode* y = x->right;

struct TreeNode* T2 = y->left;

y->left = x;

x->right = T2;

updateHeight(x);

updateHeight(y);

return y;

}

int getBalance(struct TreeNode* node) {

if (node == NULL) {

return 0;

}

return getHeight(node->left) - getHeight(node->right);

}

struct TreeNode* insertTreeNode(struct TreeNode* node, const char* key, const char* value) {

if (node == NULL) {

return createTreeNode(key, value);

}

if (strcmp(key, node->key) < 0) {

node->left = insertTreeNode(node->left, key, value);

} else if (strcmp(key, node->key) > 0) {

node->right = insertTreeNode(node->right, key, value);

} else {

strcpy(node->value, value);

return node;

}

updateHeight(node);

int balance = getBalance(node);

if (balance > 1 && strcmp(key, node->left->key) < 0) {

return rotateRight(node);

}

if (balance < -1 && strcmp(key, node->right->key) > 0) {

return rotateLeft(node);

}

if (balance > 1 && strcmp(key, node->left->key) > 0) {

node->left = rotateLeft(node->left);

return rotateRight(node);

}

if (balance < -1 && strcmp(key, node->right->key) < 0) {

node->right = rotateRight(node->right);

return rotateLeft(node);

}

return node;

}

const char* getTreeNodeValue(struct TreeNode* node, const char* key) {

if (node == NULL) {

return NULL;

}

if (strcmp(key, node->key) < 0) {

return getTreeNodeValue(node->left, key);

} else if (strcmp(key, node->key) > 0) {

return getTreeNodeValue(node->right, key);

} else {

return node->value;

}

}

int main() {

struct TreeNode* root = NULL;

root = insertTreeNode(root, "name", "Alice");

root = insertTreeNode(root, "age", "30");

const char* name = getTreeNodeValue(root, "name");

const char* age = getTreeNodeValue(root, "age");

if (name != NULL) {

printf("name: %sn", name);

} else {

printf("Key 'name' not found.n");

}

if (age != NULL) {

printf("age: %sn", age);

} else {

printf("Key 'age' not found.n");

}

return

相关问答FAQs:

1. 如何在C语言中创建一个字典?

在C语言中,可以使用结构体来创建一个字典。首先,定义一个结构体来表示字典中的每个键值对,结构体中包含一个键和一个值的变量。然后,可以使用数组来存储多个键值对,每个元素都是一个结构体实例。通过使用结构体和数组,就可以创建一个简单的字典。

2. 如何向C语言字典中添加新的键值对?

要向C语言字典中添加新的键值对,首先需要找到一个空的位置来存储新的键值对。可以通过遍历字典的数组来查找空的位置,一旦找到空的位置,就可以将新的键值对赋值给该位置的结构体实例。如果没有空的位置,可能需要重新分配更大的内存空间来容纳新的键值对。

3. 如何在C语言字典中查找特定的键值对?

要在C语言字典中查找特定的键值对,可以使用循环遍历字典的数组,并逐个比较每个元素的键与目标键是否匹配。如果找到匹配的键值对,可以返回该键值对的值。如果循环结束后仍然没有找到匹配的键值对,可以返回一个特定的值或者进行其他处理,以表示未找到该键值对。

文章包含AI辅助创作,作者:Edit1,如若转载,请注明出处:https://docs.pingcode.com/baike/1313922

(0)
Edit1Edit1
免费注册
电话联系

4008001024

微信咨询
微信咨询
返回顶部