implement linked list using array - advantages & disadvantages

后端 未结 4 910
轮回少年
轮回少年 2020-12-16 23:38

I know how to implement linked list using array. For example we define a struct as follow:

struct Node{
    int data;
    int link;
}

\"dat

4条回答
  •  Happy的楠姐
    2020-12-16 23:45

    Can anybody tell me what is the advantage and disadvantage of implementation of linked list using array compared to "ordinary" linked list?

    linked lists have the following complexity:

    • cons x xs : O(1)
    • append n m : O(n)
    • index i xs : O(n)

    if your representation uses a strict, contiguous array, you will have different complexity:

    • cons will require copying the old array: O(n)
    • append will require copying both arrays into a new contiguous space: O(n + m)
    • index can be implemented as array access: O(1)

    That is, a linked list API implemented in terms of arrays will behave like an array.

    You can mitigate this somewhat by using a linked list or tree of strict arrays, leading to ropes or finger trees or lazy sequences.

提交回复
热议问题