我开始在C中使用链表,我发现了一些问题,(如果我理解得很好)在不知道内部结构(字段)的情况下操作链表!
给定一个指向链表的指针,在不知道链表的内部结构**(不透明)的情况下,是否可以在链表中添加/删除一个项**?
编辑(添加详细信息)。
因此,问题是创建一组函数来操作链表,给定链表上的一个句柄作为参数,该参数按以下方式声明:
typedef struct list *handler;
例如我创建了一个函数来创建一个链表:
handler ListCreate()
{
handler list = (handler)malloc(sizeof(handler));
if(!list)
{
printf("can not allocate memory \n");
return NULL;
}
return list;
}
但是当涉及到附加,我只是阻止,我认为这是不可能做到的,但也许我错了。
这是函数的原型:
int ListAppend(handler list, void *item)
1条答案
按热度按时间tjjdgumg1#
我想做一些类似的事情,但我必须自己解决。想法是创建一个不透明的对象接口,然后通过使用switch语句强制转换来访问实现文件的属性。我这样做是为了遵循依赖反转原则。所有代码都在一个文件中,以显示它可以编译,但三个注解行//interface,//dependency,和//context可以分解为不同的文件,因此您可以尝试依赖文件的不同实现,而不必更改接口文件。