implement linked list using array - advantages & disadvantages

后端 未结 4 919
轮回少年
轮回少年 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条回答
  •  情书的邮戳
    2020-12-17 00:09

    Using Array implementation, you can have sequential & faster access to nodes of list, on the other hand, If you implement Linked list using pointers, you can have random access to nodes. Array implementation is helpful when you are dealing with fixed no. Of elements because resizing an array is expensive as far as performance is concerned because if you are required to insert/delete nodes from middle of the list it you have to shift every node afterwise. Contrary to this, You should use pointer implemention when you don't know no. of nodes you would want, as such a list can grow/shrink efficiently & you don't need to shift any nodes, it can be done by simply dereferencing & referencing pointers.

提交回复
热议问题