Javatpoint Logo
Javatpoint Logo

Arraylist vs linked list

Introduction:

In the world of programming, data structures play a pivotal role in organizing and managing data efficiently. Among the most commonly used data structures in many programming languages are ArrayList and LinkedList. These two data structures serve similar purposes, but they have distinct differences in terms of performance, memory usage, and use cases.

What is an ArrayList?

An ArrayList is a dynamic array-based data structure. It is part of the standard Java Collections Framework and is available in many other programming languages as well. An ArrayList can dynamically resize its capacity, allowing for the addition and removal of elements without requiring a fixed size during initialization.

Internal Mechanism:

Internally, an ArrayList uses a regular array to store elements. When the number of elements exceeds the current capacity of the array, it automatically resizes itself by creating a new array with a larger size and copying all elements from the old array to the new one. This resizing process incurs a small overhead, but it ensures that ArrayList can efficiently manage varying numbers of elements.

Program:

Explanation:

  • The program defines the main function, which serves as the entry point of the program.
  • Inside the main function, a std::vector<int> arrayList is declared, which is used to store a list of integers.
  • The program proceeds to add elements to the arrayList using the push_back Three integers, 10, 20, and 30, are added to the arrayList.
  • Next, the program demonstrates accessing elements from the arrayList using index-based random access.
  • In this case, the element at index 1 (which is the second element, since indices are 0-based) is accessed and printed to the console using std::cout.
  • The program then proceeds to iterate through the arrayList using a range-based for loop.
  • This loop iterates over each element in the arrayList one by one, and the loop variable i takes the value of each element in the iteration. Inside the loop, each element is printed to the console, separated by spaces.
  • After printing all the elements of the arrayList, the program adds a newline (std::endl) to the console to end the output.

Program Output:

Arraylist vs linked list

Advantages of ArrayList:

  • Fast Random Access: ArrayList provides constant-time access to elements by their index. Since elements are stored in a contiguous block of memory, accessing an element using its index is a direct operation, resulting in O(1) time complexity.
  • Efficient Memory Allocation: ArrayList uses a single continuous block of memory to store elements. This feature reduces memory overhead and allows for better cache performance, as elements are stored adjacently in memory.

Disadvantages of ArrayList:

Costly Insertion and Deletion: Inserting or removing elements in the middle of an ArrayList can be expensive. When an element is inserted or deleted, all subsequent elements need to be shifted, resulting in an O(n) time complexity.

What is a LinkedList?

A LinkedList is a data structure composed of a series of nodes, where each node contains an element and a reference (or pointer) to the next node in the sequence. Unlike ArrayList, LinkedList does not use contiguous memory allocation. Instead, it allows elements to be scattered across different memory locations, and the nodes are connected using pointers.

Internal Mechanism:

Each node in a LinkedList contains two parts: the data (element) and a reference to the next node in the sequence. The last node's reference is typically set to null to indicate the end of the list.

Program:

Explanation:

  • The program defines the main function, which serves as the entry point of the program.
  • Inside the main function, a std::list<int> named linkedList is declared, which will be used to store a linked list of integers.
  • The program proceeds to add elements to the linkedList using the push_back Three integers, 10, 20, and 30, are added to the linked list using push_back, which inserts elements at the end of the list.
  • To demonstrate accessing elements from the linkedList, the program performs index-based access, although linked lists are not optimized for this type of access.
  • A variable it of type std::list<int>::iterator is created, pointing to the beginning of the list (head).
  • Then, std::advance is used to move the iterator it one position forward, simulating the access of the element at index 1 (the second element in the list). The element value is then printed to the console using std::cout.
  • The program then proceeds to iterate through the linkedList using a range-based for loop.
  • This loop iterates over each element in the linkedList one by one, and the loop variable i takes the value of each element in the iteration. Inside the loop, each element is printed to the console, separated by spaces.
  • After printing all the elements of the linkedList, the program adds a newline (std::endl) to the console to end the output.

Program Output:

Arraylist vs linked list

Advantages of LinkedList:

  • Efficient Insertion and Deletion: Adding or removing elements in a LinkedList is efficient, especially in the middle of the list. Since LinkedList only requires updating pointers, these operations have a constant time complexity of O(1).
  • Memory Flexibility: LinkedList does not require contiguous memory allocation. As a result, it can efficiently utilize memory for elements of varying sizes, and there is no need for resizing operations like in ArrayList.

Disadvantages of LinkedList:

  • Slow Random Access: Unlike ArrayList, LinkedList does not provide direct index-based access to elements. To access an element at a specific index, the LinkedList must traverse from the beginning of the list until the desired element is reached. This results in an O(n) time complexity for accessing elements by index.
  • Increased Memory Overhead: LinkedList requires additional memory to store the pointers/references for each element. This overhead can be significant, especially when the number of elements is large.

Performance Comparison:

Now, let's compare the performance of ArrayList and LinkedList in different scenarios:

Random Access:

ArrayList: O(1) - Constant time, as accessing an element by index involves simple pointer arithmetic.

LinkedList: O(n) - Linear time, since it requires traversing the list from the beginning to reach the desired index.

Insertion at the End:

ArrayList: O(1) amortized - Most of the time, adding an element at the end involves appending it directly to the dynamic array, which takes constant time. Occasionally, the ArrayList might need to resize, which takes O(n) time, but this happens infrequently on average.

LinkedList: O(1) - Adding an element at the end involves creating a new node and updating the last node's next pointer, which takes constant time.

Insertion in the Middle:

ArrayList: O(n) - Inserting an element in the middle requires shifting all the subsequent elements one position to the right, which takes linear time.

LinkedList: O(1) - Inserting an element in the middle involves creating a new node, updating pointers, and takes constant time.

Deletion at the End:

ArrayList: O(1) - Removing the last element simply involves decrementing the size of the ArrayList, which takes constant time.

LinkedList: O(n) - To delete the last element, the LinkedList needs to traverse the list to find the second-to-last node, which takes linear time.

Deletion in the Middle:

ArrayList: O(n) - Deleting an element from the middle requires shifting all the subsequent elements one position to the left, which takes linear time.

LinkedList: O(1) - Deleting an element in the middle involves updating pointers, and takes constant time once the node to be deleted is located.

Use Cases:

Choose ArrayList when:

You require fast random access to elements using index-based retrieval.

The size of the list is known or does not change frequently, allowing you to set an initial capacity to minimize resizing operations.

Memory Memory consumption is a concern, as ArrayList generally uses less memory than LinkedList.

Choose LinkedList when:

Frequent insertion or deletion of elements is required, especially in the middle of the list.

You have a large number of elements, and memory usage is not a primary concern.

You are dealing with elements of varying sizes and want to optimize memory utilization.

Conclusion:

In conclusion, the choice between ArrayList and LinkedList boils down to the specific needs and characteristics of your application. ArrayList provides fast access times through index-based access (O(1)) and is suitable for read-heavy or search-intensive tasks. However, it suffers from slower insertions and deletions (O(n)) when elements are added or removed in the middle or at the beginning of the list.

On the other hand, LinkedList offers fast insertions and deletions (O(1)) by simply rearranging the node references. This makes it ideal for scenarios that involve frequent modifications to the list. However, it has slower access times (O(n)) since elements must be traversed sequentially to find a specific element.

If your application prioritizes fast read operations and random access, ArrayList might be the better choice. Conversely, if you require efficient insertions and deletions and can tolerate slower access times, LinkedList could be more suitable. Keep in mind that memory consumption should also be considered, as LinkedList incurs a higher overhead due to storing references to the next nodes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA