What is the advantage of linked list over an array and vice versa?

前端 未结 6 2015
南旧
南旧 2021-01-30 18:12

Please explain what is the advantage of linked list over an array. And also is there any advantage of using array compared to linked list.

Regards, Shoaib

6条回答
  •  醉梦人生
    2021-01-30 18:36

    Since you tagged the question with "data structures" I will answer this in that context.

    An array is fixed in size when you declare/create it meaning you can't add more elements to it. Thus, if I have an array of, say, 5 elements, you can do whatever you want with it but you can't add more elements to it.

    A linked-list is basically a way to represent a list where you can have as many "items" as you'd like. It consists of a head (the first element), a tail (the last element), and elements ( called nodes ) in-between the list.

    There are many types of linked-lists that you will probably encounter in any data structures class.

    The key thing you will learn with linked-lists is proficiency when learning how to create fields in your classes to point to other objects, which is the case for linked-lists where you need to construct your list such that each node points to the next node.

    Obviously, this is a very generalized answer. It should give you an idea for your class.

提交回复
热议问题