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 speed of accessing elements.
How does the way data is stored impact the time it takes to find or use elements?
Analyze the time complexity of accessing elements in array and linked list.
// Access element at index i in array
int getArrayElement(int arr[], int i) {
return arr[i];
}
// Access element at index i in linked list
int getLinkedListElement(Node* head, int i) {
Node* current = head;
int count = 0;
while (current != NULL && count < i) {
current = current->next;
count++;
}
if (current == NULL) return -1; // not found
return current->data;
}
This code shows how we get the element at position i in an array and in a linked list.
Look at what repeats when accessing elements.
- Primary operation: For array, direct access with no loop; for linked list, a loop moves through nodes.
- How many times: Array accesses once; linked list loops i times to reach element.
Accessing an element in an array stays fast no matter the size.
Accessing an element in a linked list takes longer as the position i grows.
| Input Size (n) | Array Access Steps | Linked List Access Steps (worst case i=n) |
|---|---|---|
| 10 | 1 | 10 |
| 100 | 1 | 100 |
| 1000 | 1 | 1000 |
Pattern observation: Array access time stays the same; linked list access time grows linearly with position.
Time Complexity: O(1) for array access, O(n) for linked list access
Array access is instant because elements are stored together; linked list access takes longer because we must follow links one by one.
[X] Wrong: "Accessing any element in a linked list is as fast as in an array."
[OK] Correct: Linked lists store elements separately, so you must move through nodes one at a time, making access slower for far elements.
Understanding these differences helps you choose the right data structure and explain your choices clearly in interviews.
"What if the linked list was doubly linked? How would that affect the time complexity of accessing elements?"
Practice
Solution
Step 1: Understand array memory layout
Arrays store elements in contiguous memory blocks, meaning all elements are next to each other.Step 2: Understand linked list memory layout
Linked list nodes are allocated separately and linked by pointers, so they can be scattered anywhere in memory.Final Answer:
Array elements are stored in contiguous memory locations, while linked list nodes are scattered in memory. -> Option BQuick Check:
Array = contiguous, Linked List = scattered [OK]
- Thinking linked list nodes are stored contiguously
- Confusing array and linked list memory layouts
- Assuming both use the same memory pattern
Solution
Step 1: Check pointer to next node type
The next pointer must be of type 'struct Node*' to point to the next node's address.Step 2: Validate struct syntax
Using 'struct Node* next;' inside the struct is correct. Using 'Node* next;' without typedef before is invalid here.Final Answer:
typedef struct Node { int data; struct Node* next; } Node; -> Option AQuick Check:
Pointer to struct Node = struct Node* [OK]
- Using 'int* next' instead of pointer to struct
- Using 'Node* next' before typedef is complete
- Declaring 'struct Node next' causing infinite size
int arr[3] = {10, 20, 30};
struct Node {
int data;
struct Node* next;
};
struct Node n1 = {10, NULL};
struct Node n2 = {20, NULL};
n1.next = &n2;
printf("%d %d\n", arr[1], n1.next->data);
What is the output?Solution
Step 1: Identify arr[1] value
arr[1] is the second element of array, which is 20.Step 2: Identify n1.next->data value
n1.next points to n2, whose data is 20.Final Answer:
20 20 -> Option AQuick Check:
arr[1] = 20, n1.next->data = 20 [OK]
- Confusing array index starting at 0
- Misreading pointer access syntax
- Assuming n1.next->data is 10 instead of 20
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int val) {
struct Node node;
node.data = val;
node.next = NULL;
return &node;
}Solution
Step 1: Analyze variable lifetime
The variable 'node' is local to createNode function and is destroyed when function returns.Step 2: Understand pointer return issue
Returning address of local variable leads to invalid pointer and undefined behavior.Final Answer:
Returning address of a local variable which becomes invalid after function ends. -> Option CQuick Check:
Local variable address returned = invalid pointer [OK]
- Assuming pointer remains valid after function ends
- Ignoring need for dynamic memory allocation
- Confusing pointer types in return
- Array
- Linked List
Solution
Step 1: Consider insertion and deletion costs
Arrays require shifting elements for insert/delete in middle, which is slow.Step 2: Consider linked list advantages
Linked lists allow quick insert/delete by changing pointers without shifting elements.Step 3: Consider memory layout tradeoff
Linked list nodes are scattered, which may reduce cache speed but improve insertion/deletion speed.Final Answer:
Linked List, because scattered nodes allow fast insertions and deletions anywhere. -> Option DQuick Check:
Fast insert/delete middle = Linked List [OK]
- Choosing array for fast insert/delete in middle
- Ignoring pointer overhead in linked list
- Assuming linked list stores elements contiguously
Ready to prove it?
Pass the test and this pattern moves unknown
