1.

When should you use an ArrayList and when a Linked List? How does a singly linked list differ from a doubly-linked list?

Answer»

An ArrayList is a collection of homogenous data types stored in contiguous memory locations while a LinkedList is a collection of data elements linked to each other using pointers. Since pointers are used the elements are not REQUIRED to occupy contiguous memory locations. Let’s see how this arrangement affects searching, insertion and DELETION of data.

  • Insertion and Deletion of elements is much easier in a LinkedList. This is because when a new node is added or existing node removed, only the pointers need to be rearranged. No shifting of data is required. HOWEVER, when an ELEMENT is added in the mid of anArrayList or removed from the middle, all the remaining elements need to be rearranged resulting in higher cost of operation.
  • Searching, however, is faster is anArrayList as the elements are stored in contiguous memory locations and can be looked up without having to traverse the entire list. In the case of a LinkedList, the entire list needs to be traversed until the element being SEARCHED for is found.

Hence if the application involves a number of reads than writes an ArrayList is suitable otherwise a LinkedList.

A singly LinkedList is one in which the node contains two fields – data – the actual content and – address – link to the next node. Though this type of list occupies less memory, it can be traversed in one direction only. A doubly LinkedList is one in which the node contains data and address of previous as well as the next node. This type of list can be traversed in both directions.



Discussion

No Comment Found