如何編寫泛型c?二叉樹的通用C
我開始寫一個平衡樹(替罪羊,splay,aa等)的集合,並找到很多共同點。示例是下面的破壞函數。
這樣的函數和類似的函數可以用void指針定義而不會導致「解除引用void *指針錯誤」嗎?
例破壞功能
void splay_node_linked_destroy(SplayNode **this) {
72 if(*this == NULL) {
73 return;
74 }
75 SplayNode *root = (*this)->root, *previous, *next;
76 while(root) {
77 if(previous == root->parent) {
78 // todo use funcs for comparisons for generic
79 if(root->left) {
80 next = root->left;
81 } else if(root->right) {
82 next = root->right;
83 } else {
84 next = root->parent;
85 }
86 } else if(previous == root->left) {
87 if(root->right) {
88 next = root->right;
89 } else {
90 next = root->parent;
91 }
92 } else {
93 next = root->parent;
94 }
95 previous = root;
96 if(next == root->parent) {
97 splay_node_destroy(&root);
98 // todo use callback here to make generic
99 }
100 root = next;
101 }
102 }
一般來說,可悲的是,答案是否定的。至少在標準的C,如果你不使用UB ... – Mints97