在Linux C编程中,确保数据结构中的元素不重复是一个常见的需求。以下是一些基础概念和相关的数据结构类型,以及它们的优势、应用场景和可能的解决方案。
优势:
应用场景:
示例代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct HashNode {
int key;
int value;
struct HashNode* next;
} HashNode;
typedef struct {
HashNode** table;
int size;
} HashTable;
HashTable* createHashTable(int size) {
HashTable* hashTable = (HashTable*)malloc(sizeof(HashTable));
hashTable->table = (HashNode**)malloc(sizeof(HashNode*) * size);
for (int i = 0; i < size; i++) {
hashTable->table[i] = NULL;
}
hashTable->size = size;
return hashTable;
}
unsigned int hashFunction(int key, int size) {
return abs(key) % size;
}
void insert(HashTable* hashTable, int key, int value) {
unsigned int index = hashFunction(key, hashTable->size);
HashNode* newNode = (HashNode*)malloc(sizeof(HashNode));
newNode->key = key;
newNode->value = value;
newNode->next = hashTable->table[index];
hashTable->table[index] = newNode;
}
int search(HashTable* hashTable, int key) {
unsigned int index = hashFunction(key, hashTable->size);
HashNode* node = hashTable->table[index];
while (node != NULL) {
if (node->key == key) {
return node->value;
}
node = node->next;
}
return -1; // Not found
}
void delete(HashTable* hashTable, int key) {
unsigned int index = hashFunction(key, hashTable->size);
HashNode* node = hashTable->table[index];
HashNode* prev = NULL;
while (node != NULL && node->key != key) {
prev = node;
node = node->next;
}
if (node == NULL) return; // Not found
if (prev == NULL) {
hashTable->table[index] = node->next;
} else {
prev->next = node->next;
}
free(node);
}
int main() {
HashTable* hashTable = createHashTable(10);
insert(hashTable, 1, 100);
insert(hashTable, 2, 200);
printf("Value for key 1: %d\n", search(hashTable, 1));
delete(hashTable, 1);
printf("Value for key 1 after deletion: %d\n", search(hashTable, 1));
return 0;
}
优势:
应用场景:
示例代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct SetNode {
int value;
struct SetNode* next;
} SetNode;
typedef struct {
SetNode* head;
} Set;
Set* createSet() {
Set* set = (Set*)malloc(sizeof(Set));
set->head = NULL;
return set;
}
void insertSet(Set* set, int value) {
if (searchSet(set, value)) return; // Avoid duplicates
SetNode* newNode = (SetNode*)malloc(sizeof(SetNode));
newNode->value = value;
newNode->next = set->head;
set->head = newNode;
}
int searchSet(Set* set, int value) {
SetNode* node = set->head;
while (node != NULL) {
if (node->value == value) {
return 1; // Found
}
node = node->next;
}
return 0; // Not found
}
void deleteSet(Set* set, int value) {
SetNode* node = set->head;
SetNode* prev = NULL;
while (node != NULL && node->value != value) {
prev = node;
node = node->next;
}
if (node == NULL) return; // Not found
if (prev == NULL) {
set->head = node->next;
} else {
prev->next = node->next;
}
free(node);
}
int main() {
Set* set = createSet();
insertSet(set, 1);
insertSet(set, 2);
insertSet(set, 1); // Duplicate, will be ignored
printf("Contains 1: %d\n", searchSet(set, 1));
deleteSet(set, 1);
printf("Contains 1 after deletion: %d\n", searchSet(set, 1));
return 0;
}
优势:
应用场景:
示例代码:
#include <stdio.h>
#include <stdlib.h>
typedef enum { RED, BLACK } Color;
typedef struct RBNode {
int value;
Color color;
struct RBNode* left;
struct RBNode* right;
struct RBNode* parent;
} RBNode;
typedef struct {
RBNode* root;
RBNode* nil; // Sentinel node
} RBTree;
RBTree* createRBTree() {
RBTree* tree = (RBTree*)malloc(sizeof(RBTree));
tree->nil = (RBNode*)malloc(sizeof(RBNode));
tree->nil->color = BLACK;
tree->root = tree->nil;
return tree;
}
void leftRotate(RBTree* tree, RBNode* x) {
RBNode* y = x->right;
x->right = y->left;
if (y->left != tree->nil) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == tree->nil) {
tree->root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
void rightRotate(RBTree* tree, RBNode* y) {
RBNode* x = y->left;
y->left = x->right;
if (x->right != tree->nil) {
x->right->parent = y;
}
x->parent = y->parent;
if (y->parent == tree->nil) {
tree->root = x;
} else if (y == y->parent->right) {
y->parent->right = x;
} else {
y->parent->left = x;
}
x->right = y;
y->parent = x;
}
void insertFixup(RBTree* tree, RBNode* z) {
while (z->parent->color == RED) {
if (z->parent == z->parent->parent->left) {
RBNode* y = z->parent->parent->right;
if (y->color == RED) {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->right) {
z = z->parent;
leftRotate(tree, z);
}
z->parent->color = BLACK;
z->parent->parent->color = RED;
rightRotate(tree, z->parent->parent);
}
} else {
RBNode* y = z->parent->parent->left;
if (y->color == RED) {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->left) {
z = z->parent;
rightRotate(tree, z);
}
z->parent->color = BLACK;
z->parent->parent->color = RED;
leftRotate(tree, z->parent->parent);
}
}
}
tree->root->color = BLACK;
}
void insertRBTree(RBTree* tree, int value) {
RBNode* z = (RBNode*)malloc(sizeof(RBNode));
z->value = value;
z->color = RED;
z->left = tree->nil;
z->right = tree->nil;
RBNode* y = tree->nil;
RBNode* x = tree->root;
while (x != tree->nil) {
y = x;
if (z->value < x->value) {
x = x->left;
} else {
x = x->right;
}
}
z->parent = y;
if (y == tree->nil) {
tree->root = z;
} else if (z->value < y->value) {
y->left = z;
} else {
y->right = z;
}
insertFixup(tree, z);
}
int main() {
RBTree* tree = createRBTree();
insertRBTree(tree, 10);
insertRBTree(tree, 20);
insertRBTree(tree, 5);
// Additional functions for search and delete can be implemented similarly
return 0;
}
问题:不同的键可能映射到相同的哈希值,导致冲突。 解决方法:
问题:插入和删除操作可能导致树失去平衡。 解决方法:
问题:动态分配的内存需要正确释放,否则可能导致内存泄漏。 解决方法:
通过合理选择和使用这些数据结构,可以有效解决Linux C编程中不重复数据的需求。
领取专属 10元无门槛券
手把手带您无忧上云