A Programmer’s approach of looking at Array vs. Linked List

Arrays are generally considered data structures that are fixed in size at compile time and whose array memory is allocated from a data section (such as a global array) or a stack section (such as a local array).

Similarly, a linked list is considered a data structure that does not have a fixed size and is allocated heap section memory as needed (using malloc (), etc.). In this sense, arrays are considered static data structures (in the data or stack section) and linked lists are considered dynamic data structures (in the stack section). The in-memory representation of matrices and linked lists can be viewed as follows:

An array of four elements (integer type) initialized with 1, 2, 3, and 4. Suppose these elements map to memory addresses 0x100, 0x104, 0x108, and 0x10C, respectively.

array

A linked list of 4 nodes where each node has an integer as data and this data is initialized with 1, 2, 3 and 4. Suppose these nodes are allocated via malloc () and the memory allocated to them is 0x200, 0x308, 0x404, and 0x20B, respectively.

array

Since the elements of the array are contiguous in memory, you can use the index to randomly access any element. intArr [3] directly accesses the fourth element of the array. (For beginners, array indexing starts at 0, so the fourth item is indexed at 3.) Furthermore, the contiguous memory of contiguous elements in the array eliminates the need to store additional information about individual elements. That is, there is no metadata overhead in the array. In contrast, linked list nodes are not contiguous in memory. This means that some mechanism is needed to traverse or access the linked list node. To accomplish this, each node stores the location of the next node, which forms the basis of the link from one node to the next. Therefore, this is called a linked list.

Now suppose you need to store certain data in an array (because the array has random access properties due to contiguous memory), but you don’t know the total size beforehand. One possibility is to allocate memory for this array from the heap at run time. For instance:

/ * Suppose you know at run time the size you need for an array of array integers, such as the size of its input. For example, the array size of the array is stored in the variable arrSize. Map this array from the heap as follows * /

int * dynArr = (int *)malloc(sizeof(int)*arrSize);

Now we need to store the data in the linked list (because the number of nodes in the linked list is equal to the actual stored data items, that is, there is no extra space as an array), but this memory Consider the case where it is not lets you get it. Stack each node many times. Suppose the type of node in the linked list (that is, the underlying array) is declared as follows:

struct sllNode
{
int dataInt;
int nextIndex;
};
struct sllNode arrayLL[5];

Both arrays and linked lists are designed to store multiple items of the same type in most cases. According to the Computer Desktop Encyclopedia, arrays are an ordered arrangement of data items that are accessed by a reference index. A linked list is a group of items, each item contains a pointer to the next item.

This article will help you determine the optimal framework for your application by examining the characteristics of each framework.

Memory allocation:

In most cases, the array is static and its size is defined at creation time. Also, the memory allocated to the array is contiguous. The downside to this approach is that large arrays require large amounts of memory and can be left unused. It is often not close to capacity, especially since it is designed for the maximum number of items. Also, on some platforms, such as certain portable devices that use older operating systems, memory limitations can limit the size of the array that can be used.

You can zoom in and out as needed during runtime. This property makes linked lists more attractive when the number of items is unknown. Also, linked list memory is allocated item by item, so it is seldom contiguous. The downside of dealing with uncertainty is that adding and removing items from a linked list requires more overhead than simply assigning values ​​to pre-mapped array items. However, the only limit on the amount of memory that can be allocated to a linked list is imposed by the size of the memory heap used by the application.

Accessing elements:

The array accessed by array index. Therefore, access to the data is easy and fast if you know what you want to get. If you don’t know the index of the required item, but the items are sorted based on some key value, you can run a very efficient search algorithm to find a particular item. These algorithms allow you to find unique items with a minimal number of comparisons. There are also some established and efficient algorithms for ordering and merging matrices. However, arrays are ineffective if the order of the elements can change. To keep an array ordered when removing or inserting elements, you may need to transfer all the elements in the array.

Linked lists are generally traversed item by item until a match is found. This list traversal is the only way to search the list, as the memory of the linked list is not guaranteed to be contiguous (no need to use any other data structure as an index). The advantage of non-contiguous memory is that sorting the list only involves hook operations. You just need to change some pointers to insert or delete items. No actual data transfer required.

Conclusion:

This article will help you determine the optimal framework for your application by examining the characteristics of each framework. Array means array contiguous memory. It can reside in any section of memory, such as data, heap, heap, etc. A linked list means non-contiguous linked memory. It can reside in any section of memory, such as heap, data, stack, etc. As a programmer, looking at data structures from the perspective of memory gives you information to choose a particular data structure or to design a new data structure. For example, you can create an array as a linked list.

Recommended Articles:

  1. How to swap nodes in a linked list without Swapping data?
  2. How to write C functions that modify head pointer of a Linked List?
  3. Find Length of a Linked List (Iterative and Recursive)?
  4. How to delete a node in a linked list at a given Location?
  5. Differences between Arrays and Linked Lists?

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 62 times, 1 visits today)

0 0 vote
Article Rating
Subscribe
Notify of
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

trackback

[…] A Programmer’s approach of looking at Array vs. Linked List […]

You cannot copy content of this page
Scroll Up
8
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.