int arr[6] = {10,20,30,40,50,60};
Visually a memory allocated array looks like this.
As a list, it might look like this:
Worst Case Array List
Space O(n) O(n) Traversal O(n) O(n) Lookup O(1) O(n) Append O(1) O(n) Prepend O(n) O(1) Insert O(n) O(n) Delete O(n) O(n)
struct Array {
int* items;
size_t length;
size_t capacity;
}
void append(struct Array arr, int value) {
if (arr.length == arr.capacity) {
// resize array
int* new_arr = malloc(sizeof(int) * capacity * 2);
for (int i = 0; i < arr.length; i++) {
[i] = arr[i];
new_arr}
(arr->items);
free->items = new_arr;
arr->items[length] = value;
arr.length++;
arr.capacity *= 2;
arr} else {
// don't resize array.
.items[length] = value;
arr.length++;
arr}
}