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

前端 未结 6 2017
南旧
南旧 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:37

    Advantages of Array over Linked List

    1. The array has a specific address for each element stored in it and thus we can access any memory directly.

    2. As we know the position of the middle element and other elements are easily accessible too, we can easily perform BINARY SEARCH in the array.

    Disadvantages of Array over Linked List

    1. Total number of elements need to be mentioned or the memory allocation needs to be done at the time of array creation

    2. The size of the array, once mentioned, cannot be increased in the program. If the number of elements entered exceed the size of the array ARRAY OVERFLOW EXCEPTION occurs.

    Advantages of Linked List over Array

    1. Size of the list doesn't need to be mentioned at the beginning of the program.

    2. As the linked list doesn't have a size limit, we can go on adding new nodes (elements) and increasing the size of the list to any extent.

    Disadvantages of Linked List over Array

    1. Nodes do not have their own address. Only the address of the first node is stored and in order to reach any node, we need to traverse the whole list from beginning to the desired node.

    2. As all Nodes don't have their particular address, BINARY SEARCH cannot be performed.

提交回复
热议问题