chapter one
1 Programming
1.1 Algorithms and Data Structures
1.1.1 Arrays vs. Linked Lists
- An array is a collection of elements stored contiguously in memory, meaning each element is stored next to the other. There are two types of arrays to consider:
- Fixed-size arrays: Adding one element requires recreating a whole new array by allocating a new array in memory and copying over all the elements.
- Dynamic arrays: Many programming languages offer a more dynamic version than fixed-size arrays, for example
ArrayListin Java,std::vectorin C++, or slices in Go. Dynamic arrays typically allocate extra space to avoid resizing on every insertion. - A linked list consists of nodes where each node contains two things: some data and a pointer (also called reference) to the next node in the list. It’s important to note that linked lists have a memory overhead compared to arrays because each node needs extra space for the pointer. A specialized version of the linked list is the doubly linked list, which has pointers to both the previous and the next node.
The main differences: