Memory Layout Comparison Array vs Linked List in DSA C++ - Complexity Comparison
We want to understand how the memory layout of arrays and linked lists affects the time it takes to access elements.
How does the way data is stored change the speed of operations?
Analyze the time complexity of accessing elements in array and linked list.
// Access element at index i in array
int getFromArray(int arr[], int i) {
return arr[i];
}
// Access element at index i in linked list
int getFromList(Node* head, int i) {
Node* current = head;
for (int count = 0; count < i; count++) {
current = current->next;
}
return current->data;
}
This code shows how we get the i-th element from an array and from a linked list.
Look at what repeats when accessing elements.
- Primary operation: Accessing element at index i.
- Array: Direct access, no loop needed.
- Linked list: Loop runs i times to reach the element.
- Dominant operation: Loop traversal in linked list.
Access time changes differently for array and linked list as i grows.
| Index (i) | Array Access Steps | Linked List Access Steps |
|---|---|---|
| 10 | 1 step (direct) | 10 steps (loop 10 times) |
| 100 | 1 step (direct) | 100 steps (loop 100 times) |
| 1000 | 1 step (direct) | 1000 steps (loop 1000 times) |
Pattern observation: Array access stays constant time no matter the index, linked list access time grows linearly with index.
Time Complexity: O(1) for array, O(n) for linked list
This means arrays let you get any element quickly, while linked lists take longer the further you go.
[X] Wrong: "Linked lists are always faster because they use pointers and don't need contiguous memory."
[OK] Correct: While linked lists are flexible in memory, accessing elements requires walking through nodes one by one, making access slower than arrays.
Understanding how memory layout affects access speed helps you choose the right data structure for your problem and explain your choice clearly in interviews.
What if we added an index or hash map to the linked list to speed up access? How would that change the time complexity?
Practice
array compared to a linked list?Solution
Step 1: Understand array memory layout
Arrays store elements in contiguous memory blocks, meaning elements are placed one after another in memory.Step 2: Understand linked list memory layout
Linked list nodes are allocated dynamically and can be anywhere in memory, connected by pointers.Final Answer:
Array elements are stored in contiguous memory locations, while linked list nodes are scattered in memory. -> Option DQuick Check:
Array = contiguous, Linked List = scattered [OK]
- Thinking linked list nodes are contiguous
- Confusing array with linked list memory layout
- Assuming both have same memory layout
Solution
Step 1: Understand pointer usage in struct
The next node pointer must be a pointer type to allow linking nodes dynamically.Step 2: Analyze each option
struct Node { int data; Node next; }; uses Node next (not pointer), invalid. struct Node { int data; Node* next; }; uses Node* next (correct pointer). struct Node { int data; Node& next; }; uses reference Node& next (not suitable for linked list). struct Node { int data; int* next; }; uses int* next (wrong type).Final Answer:
struct Node { int data; Node* next; }; -> Option AQuick Check:
Pointer to next node = Node* next [OK]
- Using object instead of pointer for next
- Using reference instead of pointer
- Using wrong pointer type
int arr[3] = {10, 20, 30};
struct Node { int data; Node* next; };
Node* n1 = new Node{10, nullptr};
Node* n2 = new Node{20, nullptr};
Node* n3 = new Node{30, nullptr};
n1->next = n2; n2->next = n3;
std::cout << &arr[0] << " " << &arr[1] << " " << &arr[2] << std::endl;
std::cout << n1 << " " << n2 << " " << n3 << std::endl;Solution
Step 1: Analyze array memory addresses
Array elements are stored contiguously, so &arr[0], &arr[1], &arr[2] will be adjacent addresses.Step 2: Analyze linked list node addresses
The nodes are separately allocated dynamically with new; their addresses (printed as n1, n2, n3) are non-contiguous in memory.Final Answer:
Array addresses are contiguous; linked list addresses are non-contiguous -> Option BQuick Check:
Array contiguous, linked list scattered [OK]
- Assuming linked list nodes are contiguous
- Confusing variable addresses with pointer values
- Ignoring dynamic allocation behavior
struct Node {
int data;
Node* next;
};
Node* head = new Node{1, nullptr};
Node* second = new Node{2, nullptr};
Node* third = new Node{3, nullptr};
head->next = second;
second = third;
third->next = nullptr;Solution
Step 1: Trace pointer assignments
head->next = second links first node to second node correctly. Then 'second = third;' changes the local second pointer to point to third node, but node2->next remains nullptr. third->next = nullptr sets nothing new.Step 2: Understand impact on linked list
The next pointer of the second node is never set to the third node, so the list is 1 -> 2 -> null with third node unreachable from head.Final Answer:
The next pointers are not set correctly for all nodes. -> Option CQuick Check:
Missing second->next = third [OK]
- Confusing pointer variable with node's next pointer
- Ignoring pointer reassignment effect
- Assuming nodes are not dynamically allocated
- Array
- Linked List
Solution
Step 1: Consider access by index
Arrays allow direct access by index in O(1) time due to contiguous memory layout.Step 2: Consider memory and cache effects
Contiguous memory improves cache locality, speeding up access. Linked lists have scattered nodes, causing cache misses and slower access.Final Answer:
Array, because contiguous memory allows fast index access and better cache performance. -> Option AQuick Check:
Fast index access = Array with contiguous memory [OK]
- Assuming linked list is faster for index access
- Ignoring cache locality effects
- Confusing memory fragmentation with access speed
Ready to prove it?
Pass the test and this pattern moves unknown
