Submitted Questions

  • Arrays/Linked List

    When and on what conditions will you decide whether to use linked lists or arrays, also Which process is fastest and which is slowest in arrays as well as linked list i.e, sorting, searching, add, delete etc.

    Star Read Best Answer

    Editorial / Best Answer

    Aqan12  

    • Member Since Apr-2010 | Jun 13th, 2010


    Memory:
    When you create an array you allocate all the memory while initializing the array, so if you had an array of 1000 Objects, you'd allocate space to hold 1000 object references as soon as you initialize the array.

    LinkedList on the other hand extends dynamically as the objects are added to the linked list.

    Accessing Elements:
    Arrays are useful if you are going to access elements using the index. if you need to access the 100th element you can directly access it by a[100].

    To access 100th element in a LinkedList you'll have to traverse 99 elements to reach the 100th element.

    LinkedList can handle addition/deletion of elements better then arrays and that's one of the most powerful feature of LL over arrays.

    Adding/Inserting Elements:
    Array is fixed size; to add a number of elements greater then the size of the array you'd have to copy the array into a new bigger array.

    To insert an element in the middle of an array all the elements after the inserted element would have to moved up one index.

    LinkedList can grow dynamically.

    Deleting Elements:
    To delete an element from Array you'd have to move down all the elements after the deleted element.

    LinkedList elements can be deleted dynamically.

    To summarize, LinkedList can handle manipulation of data structure much better and efficiently manage memory but arrays have better accessibility when accessing elements by index.
    Search and Sort would be similar performance on both.