【发布时间】:2014-02-23 20:12:50
【问题描述】:
下面是我的代码,它为 C 中的链接列表提供了一个简单的接口。因此它的行为类似于 java 中的 ArrayLists。
我的问题是这样的:
我的代码只适用于希望拥有整数链接列表的人。
我知道他们可以使用 int 通过指针来保存其数据类型的地址。
但是,我想要更通用的东西。
我可以在节点结构中使用 void* 代替 int 吗?那么用户可以提供int、double、char等...?
代码:
#include <stdio.h>
#include <stdlib.h>
int AL_appened(int val);
struct Tuple AL_find(int val);
int AL_remove(int val);
int AL_setup();
int AL_len();
typedef struct Node Node;
typedef struct Tuple Tuple;
struct Node{
int val;
Node *next;
};
struct Tuple{
int index;
int val;
};
Node *root, *curr;
int AL_appened(int val) {
Node *tmp;
tmp = (Node *)malloc(sizeof (Node));
curr->next = tmp;
tmp->val = val;
curr = tmp;
root->val++;
return 0;
}
struct Tuple AL_find(int val){
if(root->next) {
curr = root->next;
int count = 0;
while (curr->next){
if (curr->val == val){
Tuple r = {count+=1, val};
return r;
}
count++;
curr = curr->next;
}
if (curr->val == val){
Tuple r = {count+=1, val};
return r;
}
}
Tuple r = {-1, -1};
return r;
}
int AL_remove(int val){
Node *prev;
prev = (Node *)malloc(sizeof (Node));
curr = root;
while (curr->next->val != val){
prev = curr;
curr = curr->next;
}
if (curr->next->val != val) return -1;
curr->next = curr->next->next;
root->val--;
free(curr->next);
return 1;
}
int AL_setup(){
root = (Node *)malloc(sizeof(Node));
root->val = 0;
root->next = 0;
curr = root;
return 0;
}
int AL_len(){
return root->val;
}
void printAll(){
curr=root->next;
while (curr->next != NULL) {
printf("%d\n",curr->val);
curr=curr->next;
}
printf("%d\n",curr->val);
}
int main(){
AL_setup(); //setup the root, we will use root to keep track of the number of links
AL_appened(1); // append 1 so it should look like root>1
AL_appened(2); // append 2 so it should look like root>1>2
AL_appened(3);
AL_appened(4);
printf("%d\n", AL_len()); // print len of list
Tuple results = AL_find(4); // find 4 in list
printf("%d %d\n", results.index, results.val); // return the index and the number found
AL_remove(3);
Tuple results2 = AL_find(4);
printf("%d %d\n", results2.index, results2.val);
results2 = AL_find(4);
printf("%d %d\n", results2.index, results2.val);
printAll(); // print entire list
return 0;
}
【问题讨论】:
-
Doorknob:就在我投票给你的时候...... ;) C 没有模板,是吗?
-
@Doorknob 或禁言类型!但这是 C。
-
@Constantinius 哎呀,很抱歉忽略了标签:P(当我对 C++ 几乎一无所知时,我也试图回答它:P)
-
检查 klib.sourceforge.net 是否使用宏替代 void*
标签: c