Linked lists are more memory-efficient than arrays in

Linked List vs Array

Arrays store elements in contiguous memory locations, resulting in easily calculable addresses for the elements stored and this allows faster access to an element at a specific index. Linked lists are less rigid in their storage structure and elements are usually not stored in contiguous locations, hence they need to be stored with additional tagsgiving a reference to the next element. This difference in the data storage scheme decides which data structure would be more suitable for a given situation.

Data storage scheme of an array

Data storage scheme of a linked list

Major differences are listed below:

  • Size: Since data can only be stored in contiguous blocks of memory in an array, its size cannot be altered at runtime due to the risk of overwriting other data. However, in a linked list, each node points to the next one such that data can exist at scattered [non-contiguous] addresses; this allows for a dynamic size that can change at runtime.
  • Memory allocation: For arrays at compile time and at runtime for linked lists. but, a dynamically allocated array also allocates memory at runtime.
  • Memory efficiency: For the same number of elements, linked lists use more memory as a reference to the next node is also stored along with the data. However, size flexibility in linked lists may make them use less memory overall; this is useful when there is uncertainty about size or there are large variations in the size of data elements; memory equivalent to the upper limit on the size has to be allocated [even if not all of it is being used] while using arrays, whereas linked lists can increase their sizes step-by-step proportionately to the amount of data.
  • Execution time: Any element in an array can be directly accessed with its index; however in the case of a linked list, all the previous elements must be traversed to reach any element. Also, better cache locality in arrays [due to contiguous memory allocation] can significantly improve performance. As a result, some operations [such as modifying a certain element] are faster in arrays, while some others [such as inserting/deleting an element in the data] are faster in linked lists.

Following are the points in favor of Linked Lists.
[1] The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and in practical uses, the upper limit is rarely reached.



[2] Inserting a new element in an array of elements is expensive because room has to be created for the new elements and to create room existing elements have to be shifted.

For example, suppose we maintain a sorted list of IDs in an array id[ ].

id[ ] = [1000, 1010, 1050, 2000, 2040, …..].

And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 [excluding 1000].

Deletion is also expensive with arrays unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved.

So Linked list provides the following two advantages over arrays
1] Dynamic size
2] Ease of insertion/deletion

Linked lists have the following drawbacks:
1] Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do a binary search with linked lists.
2] Extra memory space for a pointer is required with each element of the list.
3] Arrays have better cache locality that can make a pretty big difference in performance.

4] It takes a lot of time in traversing and changing the pointers.

5] It will be confusing when we work with pointers.

References:
//cslibrary.stanford.edu/103/LinkedListBasics.pdf

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article Tags :

Arrays

Linked List

Practice Tags :

Linked List

Arrays

Read Full Article

Question: Are Linked Lists More Memory Efficient Than Arrays?

Feb 19 2022

▲ 15 ▼

Answer The Question

Similar Questions

  1. Why insertion is faster in linked lis
  2. What are the disadvantages of array
  3. Why is linked list preferred over arra
  4. Which is more efficient array or linked lis
  5. Are Linked lists faster than array
  6. What are the advantages and disadvantages of linked lis
  7. What is linked list and its advantage
  8. Does ArrayList maintain insertion orde
  9. Is random access allowed in linked lis
  10. Which is faster in array and ArrayLis
  11. Which takes more memory array or linked lis
  12. What are the disadvantages of linked lis
  13. What operation is least efficient in a linked lis
  14. Why do we use linked list
  15. What are the disadvantages of doubly linked lis
  16. What are the different types of linked lis
  17. When would you use a linked list vs ArrayLis
  18. Is linked list better over arra
  19. What are the pros and cons of arrays and linked lis
  20. Is ArrayList linked lis
  21. What is difference between Array and Lis

Asked By: Jayden Ramirez Date: created: Jan 15 2022

Arrays Vs Linked Lists

Arrays and Linked Lists both are linear data structures, but they both have some advantages and disadvantages over each other.

One advantage of the linked list is that elements can be added to it indefinitely, while an array will eventually get filled or have to be resized [a costly operation that isn't always possible].

Elements are also easily removed from a linked list whereas removing elements from an array leaves empty spaces that are a waste of computer memory.

Insertion in Array and Linked List

However, unlike arrays which allow random access to the elements contained within them, a link list only allows sequential access to its elements. Linked lists also use more storage space in a computer's memory as each node in the list contains both a data item and a reference to the next node.

It follows that linked lists should be used for large lists of data where the total number of items in the list is changing. Arrays, on the other hand, are better suited to small lists, where the maximum number of items that could be on the list is known.

Difference between Array and Linked List

Both Linked List and Array are used to store linear data of similar type, but an array consumes contiguous memory locations allocated at compile time, i.e. at the time of declaration of array, while for a linked list, memory is assigned as and when data is added to it, which means at runtime.

Before we proceed further with the differences between Array and Linked List, if you are not familiar with Array or Linked list or both, you can check these topics first:

  • Array in C
  • Linked List

This is the basic and the most important difference between a linked list and an array. In the section below, we will discuss this in details along with highlighting other differences.

Array vs Linked List

Array and Linked List are the two most used data structures. It's really important to understand and compare the advantages and disadvantages of both array and linked list. In this blog, we will compare these two linear data structures. So let’s get started.

To start of the things, we will take some parameters through which we can compare both the data structures.

Structure

Array and Linked list are used to store linear data of similar type but the major difference between them is related to their structure. Arrays are an index-based data structure where each element is associated with an index. On the other hand, Linked list relies on references of where the next element in the list is stored, the last element of the linked list refers to NULL denoting that its the end of the list.

Arrays

Linked List

Size

Arrays have fixed size and it is required to know the size of the array at the time of declaration whereas Linked List is not restricted to size and can be expanded during the execution. So, Linked lists are dynamic, flexible.

Memory Required

The memory required to store data in the linked list is more than that of an array because of additional memory used to store the address/references of the next node.

Storage Allocation

In an array, memory is assigned during compile time while in a Linked list it is allocated during execution or runtime.

The elements in the array are stored at contiguous positions in the memory whereas the elements in the linked list are stored at random positions.

Accessing Time

Elements in the array can be accessed using it’s index i.e. if you want to get into the fourth element you have to write the array variable name with its index or location within the square bracket. But, in a linked list, you have to start from the head and iterate until you get to the fourth element.

The elements in an array can be directly and sequentially accessed but in a linked list only sequential access is possible. To conclude, accessing an element in an array is fast and is a constant time operation whereas, in a linked list, it takes linear time.

Memory utilization

Memory utilization is inefficient in the array as we need to declare the size of the array during the time of declaration. Conversely, memory utilization is efficient in the linked list.

Insertion/ Deletion

Operations like insertion and deletion in arrays consume a lot of time as shifting of elements are required but these operations are easy, fast and efficient in linked lists.

Conclusion

From the above points, we can conclude that Array and Linked lists are the types of data structures that differ in their structure, accessing and manipulation methods, memory requirement, and utilization and have particular advantages and disadvantages over its implementation.

Linked List: Dynamic Size, Fast insertion, and deletion once the element is reached

Arrays: Fast Random element access and requires less memory per element

Suggested Problems to solve

  • Wave Array
  • Set Matrix Zeroes
  • Move zeroes to an end
  • Merge two sorted arrays
  • Remove Duplicates from Sorted List
  • Remove duplicates from sorted array
  • Find an element in Bitonic array
  • Merge Sort on Linked List

Happy coding! Enjoy Algorithms.

Array vs Linked List

Array and Linked list are the two ways of organizing the data in the memory. Before understanding the differences between the Array and the Linked List, we first look at an array and a linked list.

What is an array?

An array is a data structure that contains the elements of the same type. A data structure is a way of organizing the data; an array is a data structure because it sequentially organizes the data. An array is a big chunk of memory in which memory is divided into small-small blocks, and each block is capable of storing some value.

Suppose we have created an array that consists of 10 values, then each block will store the value of an integer type. If we try to store the value in an array of different types, then it is not a correct array and will throw a compile-time error.

Declaration of array

An array can be declared as:

To declare an array, we first need to specify the type of the array and then the array's name. Inside the square brackets, we need to specify the number of elements that our array should contain.

Let's understand through an example.

In the above case, we have declared an array of 5 elements with 'a' name of an integer data type.

What is Linked list?

A linked list is the collection of nodes that are randomly stored. Each node consists of two fields, i.e., data and link. Here, data is the value stored at that particular node, and the link is the pointer that holds the address of the next node.

Differences between Array and Linked list

We cannot say which data structure is better, i.e., array or linked list. There can be a possibility that one data structure is better for one kind of requirement, while the other data structure is better for another kind of requirement. There are various factors like what are the frequent operations performed on the data structure or the size of the data, and other factors also on which basis the data structure is selected. Now we will see some differences between the array and the linked list based on some parameters.

1. Cost of accessing an element

In case of an array, irrespective of the size of an array, an array takes a constant time for accessing an element. In an array, the elements are stored in a contiguous manner, so if we know the base address of the element, then we can easily get the address of any element in an array. We need to perform a simple calculation to obtain the address of any element in an array. So, accessing the element in an array is O[1] in terms of time complexity.

In the linked list, the elements are not stored in a contiguous manner. It consists of multiple blocks, and each block is represented as a node. Each node has two fields, i.e., one is for the data field, and another one stores the address of the next node. To find any node in the linked list, we first need to determine the first node known as the head node. If we have to find the second node in the list, then we need to traverse from the first node, and in the worst case, to find the last node, we will be traversing all the nodes. The average case for accessing the element is O[n].

We conclude that the cost of accessing an element in array is less than the linked list. Therefore, if we have any requirement for accessing the elements, then array is a better choice.

2. Cost of inserting an element

There can be three scenarios in the insertion:

  • Inserting the element at the beginning: To insert the new element at the beginning, we first need to shift the element towards the right to create a space in the first position. So, the time complexity will be proportional to the size of the list. If n is the size of the array, the time complexity would be O[n].

In the case of a linked list, to insert an element at the starting of the linked list, we will create a new node, and the address of the first node is added to the new node. In this way, the new node becomes the first node. So, the time complexity is not proportional to the size of the list. The time complexity would be constant, i.e., O[1].

  • Inserting an element at the end

If the array is not full, then we can directly add the new element through the index. In this case, the time complexity would be constant, i.e., O[1]. If the array is full, we first need to copy the array into another array and add a new element. In this case, the time complexity would be O[n].

To insert an element at the end of the linked list, we have to traverse the whole list. If the linked list consists of n elements, then the time complexity would be O[n].

  • Inserting an element at the mid

Suppose we want to insert the element at the ith position of the array; we need to shift the n/2 elements towards the right. Therefore, the time complexity is proportional to the number of the elements. The time complexity would be O[n] for the average case.

In the case of linked list, we have to traverse to that position where we have to insert the new element. Even though, we do not have to perform any kind of shifting, but we have to traverse to n/2 position. The time taken is proportional to the n number of elements, and the time complexity for the average case would be O[n].

The resultant linked list is:

  • Ease of use

The implementation of an array is easy as compared to the linked list. While creating a program using a linked list, the program is more prone to errors like segmentation fault or memory leak. So, lots of care need to be taken while creating a program in the linked list.

  • Dynamic in size

The linked list is dynamic in size whereas the array is static. Here, static doesn't mean that we cannot decide the size at the run time, but we cannot change it once the size is decided.

3. Memory requirements

As the elements in an array store in one contiguous block of memory, so array is of fixed size. Suppose we have an array of size 7, and the array consists of 4 elements then the rest of the space is unused. The memory occupied by the 7 elements:

Memory space = 7*4 = 28 bytes

Where 7 is the number of elements in an array and 4 is the number of bytes of an integer type.

In case of linked list, there is no unused memory but the extra memory is occupied by the pointer variables. If the data is of integer type, then total memory occupied by one node is 8 bytes, i.e., 4 bytes for data and 4 bytes for pointer variable. If the linked list consists of 4 elements, then the memory space occupied by the linked list would be:

Memory space = 8*4 = 32 bytes

The linked list would be a better choice if the data part is larger in size. Suppose the data is of 16 bytes. The memory space occupied by the array would be 16*7=112 bytes while the linked list occupies 20*4=80, here we have specified 20 bytes as 16 bytes for the size of the data plus 4 bytes for the pointer variable. If we are choosing the larger size of data, then the linked list would consume a less memory; otherwise, it depends on the factors that we are adopting to determine the size.

Let's look at the differences between the array and linked list in a tabular form.

ArrayLinked list
An array is a collection of elements of a similar data type.A linked list is a collection of objects known as a node where node consists of two parts, i.e., data and address.
Array elements store in a contiguous memory location.Linked list elements can be stored anywhere in the memory or randomly stored.
Array works with a static memory. Here static memory means that the memory size is fixed and cannot be changed at the run time.The Linked list works with dynamic memory. Here, dynamic memory means that the memory size can be changed at the run time according to our requirements.
Array elements are independent of each other.Linked list elements are dependent on each other. As each node contains the address of the next node so to access the next node, we need to access its previous node.
Array takes more time while performing any operation like insertion, deletion, etc.Linked list takes less time while performing any operation like insertion, deletion, etc.
Accessing any element in an array is faster as the element in an array can be directly accessed through the index.Accessing an element in a linked list is slower as it starts traversing from the first element of the linked list.
In the case of an array, memory is allocated at compile-time.In the case of a linked list, memory is allocated at run time.
Memory utilization is inefficient in the array. For example, if the size of the array is 6, and array consists of 3 elements only then the rest of the space will be unused.Memory utilization is efficient in the case of a linked list as the memory can be allocated or deallocated at the run time according to our requirement.

Next TopicStack vs. Queue



← prev next →



Video liên quan

Bài mới nhất

Chủ Đề