Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Difference between Array and Linked List

--

Two fundamental linear data structures that programmers often encounter are arrays and linked lists. Both data structures have their strengths and weaknesses. Arrays are ideal for tasks that involve frequent element access, searching, or sorting but have a fixed size that cannot change during runtime. Linked lists are more flexible and can grow and shrink as required, making them ideal for tasks that require frequent insertion or deletion of elements. It’s important to consider the task’s requirements and characteristics to determine which data structure is best suited.

happycoders: Array — singly linked list — doubly linked list

What is an array?

Arrays in C++ are collections of elements of the same type that are stored in contiguous memory locations. This means that each element in the array is stored next to each other in memory, making it easy to access each element using its index. To declare an array in C++, you can use a

  1. built-in array or
  2. std::array container from the STL

Here’s an example of an array declaration using a built-in array and standard array:

// 1. Built in Array
int myArray[5]; // declares an array of 5 integers

// 2. Standard Array
#include <array>
std::array<int, 5> myArray;

myArray[0] = 42; // sets the first element to 42
int x = myArray[0]; // gets the value of the first element (42)


// 3. Built in Array: use one ofthe two way
int myArray[5] = {1, 2, 3, 4, 5};
int myArray[5] {1, 2, 3, 4, 5};

// 4. Standard Array
std::array<int, 5> myArray {1, 2, 3, 4, 5};
takeuforward

Here is an implementation of built-in array in a program.

Here’s an example of its implementation of std:array in a program:

What is a Linked list?

Linked lists are a dynamic data structure in which each element is called a node. Each node contains a value and a reference (a pointer or a link) to the next node in the list. This means that each node in a linked list contains not only the value it stores, but also a pointer to the next node in the list.

Linked lists can be

  1. Singly linked list or
  2. Doubly linked list

In a singly linked list, each node only contains a pointer to the next node in the list. This means that you can only traverse the list in one direction — from the head (or first) node to the tail (or last) node.

geeksforgeeks

Here’s an example of a singly linked list:

In a doubly linked list, each node contains pointers to both the next and previous nodes in the list. This means that you can traverse the list in both directions — from the head to the tail, and from the tail to the head.

Here’s an example of a doubly linked list:

Linked List vs. Array:

Memory Management:
In C++, arrays are allocated as contiguous blocks of memory. The size of the array is determined at compile-time, either as a fixed constant or dynamically using dynamic memory allocation with new. Array elements are accessed using indices, allowing for constant-time access.

Linked lists are implemented as a collection of nodes, where each node contains the data and a pointer/reference to the next node. Memory for each node is dynamically allocated at runtime using new. Linked lists do not require contiguous memory blocks and can grow or shrink dynamically.

Flexibility:
Arrays have a fixed size that is determined at the time of declaration. Once allocated, the size cannot be easily changed. Resizing an array involves creating a new array and copying elements, which can be time-consuming and memory-intensive.

Linked lists offer flexibility in terms of resizing. They can grow or shrink dynamically by adding or removing nodes. This makes linked lists suitable for situations where the size of the data structure needs to change frequently.

Insertion and Deletion:
Inserting or deleting elements in an array can be challenging. Insertion requires shifting elements to accommodate the new element, while deletion requires moving subsequent elements to fill the gap. These operations have a time complexity of O(n), where n is the number of elements in the array.

Linked lists excel in insertion and deletion operations. Inserting a new node involves changing a few pointers while deleting a node requires reassigning pointers and freeing memory. These operations have a time complexity of O(1), constant time, on average, making linked lists efficient for frequent modifications.

Memory Efficiency:
Arrays are generally more memory-efficient than linked lists. They only require memory for the elements themselves, without any additional overhead. However, if the array size is significantly larger than the actual number of elements, it may result in wasted memory.

Linked lists require additional memory to store the pointers/references between nodes. This overhead makes linked lists less memory-efficient compared to arrays, especially when dealing with small amounts of data.

Access and Search:
Arrays offer direct access to elements using indices, allowing for constant-time retrieval. This makes arrays suitable for situations where random access to elements is a common requirement, such as searching for specific values.

Linked lists do not provide direct access to elements by index. To access a specific element, you need to traverse the list from the beginning. This results in a time complexity of O(n), where n is the position of the element. However, some variants of linked lists, like doubly linked lists, allow for backward traversal.

open4tech

Conclusion

Arrays provide constant-time access to elements, which makes them ideal for situations where random access to elements is important. They are also more memory-efficient than linked lists, as they store all elements in contiguous memory. However, arrays have fixed sizes and cannot be resized without allocating a new array and copying the elements over, which can be inefficient for large arrays.

Linked lists, on the other hand, provide flexibility in resizing and efficient insertion and deletion operations. They can also be used to implement other data structures, such as stacks and queues. However, linked lists have slower access times since they require traversing the list to access a specific element. Additionally, linked lists use more memory than arrays since each node requires a pointer to the next (and possibly previous) node.

When deciding which data structure to use, it’s important to consider the trade-offs between time complexity, memory efficiency, and flexibility. For tasks that require random access to elements, arrays are usually the better choice. For tasks that involve frequent resizing or insertion/deletion operations, linked lists may be the better choice. However, for some tasks, it may be necessary to use both data structures in combination to achieve the desired performance and functionality.

Leetcode Problem Lists (September 2023)

  1. Middle of the Linked List
    Using two pointers, slow and fast, where fast moves two steps forward for every one step of slow, the middle node of a linked list can be found when fast reaches the end, leaving slow at the middle node. For complete soltion please follow the following link.
    Companies with Frequency:
    Amazon: 5, Adobe: 4, Facebook: 3, Google: 3, Microsoft: 2, Apple: 2, Qualcomm: 2
  2. Reverse Linked List
    The solution uses 3 variables (prevNode, head, and nextNode) to reverse a linked list by iterating through it until the end, updating the variables with each step. The final result is stored in prevNode. For complete solution, please follwo the following link.
    Companies with Frequency:
    Amazon: 12, Microsoft: 10, Apple: 6, Bloomberg: 4, Facebook: 4, Google: 3, Yandex: 3, Intuit: 2, Nvidia: 2

Discover the game-changing benefits of std::array — the ultimate solution for structured storage of your fixed-sized arrays! Check out the link for more information.

Explores the versatile vector container in C++, discussing its features and capabilities for managing collections of elements effectively using the following link.

--

--

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Nitish Singh

Passionate about empowering devs with practical tips, using modern C++ as the tool, and up-to-date knowledge. Join me on Medium for engaging articles.

No responses yet