Linked list vs. dynamic array for implementing a stack

后端 未结 5 1087
再見小時候
再見小時候 2020-12-24 13:02

I\'ve started reviewing data structures and algorithms before my final year of school starts to make sure I\'m on top of everything. One review problem said \"Implement a s

相关标签:
5条回答
  • 2020-12-24 13:41

    What matters is the number of times malloc() gets called in the course of running a task. It could take from hundreds to thousands of instructions to get you a block of memory. (The time in free() or GC should be proportional to that.) Also, keep a sense of perspective. This might be 99% of the total time, or only 1%, depending what else is happening.

    0 讨论(0)
  • 2020-12-24 13:46

    Resizing the dynamic array would not be an expensive task if you design your implementation well.

    For instance, to grow the array, if it is full, create a new array of twice the size, and copy items.

    You will end up with an amortized cost of ~3N for adding N items.

    0 讨论(0)
  • 2020-12-24 13:51

    There are many tradeoffs involved here and I don't think that there's a "correct" answer to this question.

    If you implement the stack using a linked list with a tail pointer, then the worst-case runtime to push, pop, or peek is O(1). However, each element will have some extra overhead associated with it (namely, the pointer) that means that there is always O(n) overhead for the structure. Additionally, depending on the speed of your memory allocator, the cost of allocating new nodes for the stack might be noticeable. Also, if you were to continuously pop off all the elements from the stack, you might have a performance hit from poor locality, since there is no guarantee that the linked list cells will be stored contiguously in memory.

    If you implement the stack with a dynamic array, then the amortized runtime to push or pop is O(1) and the worst-case cost of a peek is O(1). This means that if you care about the cost of any single operation in the stack, this may not be the best approach. That said, allocations are infrequent, so the total cost of adding or removing n elements is likely to be faster than the corresponding cost in the linked-list based approach. Additionally, the memory overhead of this approach is usually better than the memory overhead of the linked list. If your dynamic array just stores pointers to the elements, then the memory overhead in the worst-case occurs when half the elements are filled in, in which case there are n extra pointers (the same as in the case when you were using the linked list), and in the best case when the dynamic array is full there are no empty cells and the extra overhead is O(1). If, on the other hand, your dynamic array directly contains the elements, the memory overhead can be worse in the worst-case. Finally, because the elements are stored contiguously, there is better locality if you want to continuously push or pop elements from the stack, since all the elements are right next to each other in memory.

    In short:

    • The linked-list approach has worst-case O(1) guarantees on each operation; the dynamic array has amortized O(1) guarantees.
    • The locality of the linked list is not as good as the locality of the dynamic array.
    • The total overhead of the dynamic array is likely to be smaller than the total overhead of the linked list, assuming both store pointers to their elements.
    • The total overhead of the dynamic array is likely to be greater than that of the linked list if the elements are stored directly.

    Neither of these structures is clearly "better" than the other. It really depends on your use case. The best way to figure out which is faster would be to time both and see which performs better.

    Hope this helps!

    0 讨论(0)
  • 2020-12-24 13:56

    I think you answered the question yourself. For a stack with a large number of items, the dynamic array would have excessive overhead costs (copying overhead) when simply adding an extra item to the top of the stack. With a list it's a simple switch of pointers.

    0 讨论(0)
  • 2020-12-24 13:57

    Well, for the small objects vs. large objects question, consider how much extra space to use for a linked list if you've got small objects on your stack. Then consider how much extra space you'll need if you've got a bunch of large objects on your stack.

    Next, consider the same questions, but with an implementation based on dynamic arrays.

    0 讨论(0)
提交回复
热议问题