在ANSI C动态列表? [英] Dynamic list in ANSI C?
问题描述
我说的对的思维创造用C在运行时它生长名单的唯一办法,就是用链表?
Am i right in thinking the only way to create a list which grows during run time in C, is to use a linked list?
推荐答案
链表是单向的。它有 O(1)
插入(假设你在插入点是已经)和删除,但 O(N)
N
'th元素查找。
Linked list is one way. It has O(1)
insertion (assuming you're already at the insertion point) and deletion, but O(n)
n
'th element lookup.
调整与的malloc
是另外一个动态分配的数组。它有 O(1)
N
'th元素查找,但<code> O(N)插入(由于有复制的插入点毕竟后面的元素,并在可能的所有元素的realloc
),并删除。
Resizing a dynamically allocated array with malloc
is another. It has O(1)
n
'th element lookup but O(n)
insertion (due to having to copy all later elements after the insertion point, and possibly all elements on realloc
) and deletion.
如果你真的很担心针对这两项操作的表现,我会去与某种自我平衡的树结构。你一定能达到 O(log n)的
的查找,插入和删除。而对于所有的实际目的,如果对象在主内存中,为log N
由32或64界和 O(log n)的
可能会成为 O(1)
。
If you're really worried about performance for both these operations, I would go with some sort of self-balancing tree structure. You can surely attain O(log n)
for lookup, insertion, and deletion. And for all practical purposes if objects are in main memory, log n
is bounded by 32 or 64 and O(log n)
might as well be O(1)
.
这篇关于在ANSI C动态列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!