30.10. Lists¶
30.10.1. Lists¶
30.10.1.1. Lists¶
A list is a finite, ordered sequence of data items.
Important concept: List elements have a position.
Notation: <a0,a1,…,an−1>
What operations should we implement?
30.10.1.2. List Implementation Concepts¶
Our list implementation will support the concept of a current position.
Operations will act relative to the current position.
<20,23 | 12,15>
30.10.1.3. List ADT (1)¶
30.10.1.4. List ADT (2)¶
30.10.1.5. List ADT (3)¶
30.10.1.6. List ADT Examples¶
List: <12 | 32,15>
L.insert(99);
Result: <12 | 99,32,15>
Iterate through the whole list:
30.10.1.7. List Find Function¶
30.10.1.8. Array-Based List Class (1)¶
30.10.1.9. Array-Based List Insert¶
30.10.1.10. Link Class¶
Dynamic allocation of new list elements.
30.10.1.11. Linked List Position (1)¶
1 / 3 Settings<<<>>>Here is a graphical depiction for a linked list storing five integers. The value stored in a pointer variable is indicated by an arrow "pointing" to something. A NULL pointer is indicated graphically by a diagonal slash through a pointer variable's box. The vertical line between the nodes labeled 23 and 10 indicates the current position (immediately to the right of this line).2023101215
30.10.1.12. Linked List Position (2)¶
30.10.1.13. Linked List Position (3)¶
nullnullheadcurrtailnull2023101215nullheadcurrtail
30.10.1.14. Linked List Class (1)¶
30.10.1.15. Linked List Class (2)¶
30.10.1.16. Insertion¶
30.10.1.17. Removal¶
30.10.1.18. Prev¶
30.10.1.19. Overhead¶
Container classes store elements. Those take space.
Container classes also store additional space to organize the elements.
- This is called overhead
The overhead fraction is: overhead/total space
30.10.1.20. Comparison of Implementations¶
- Array-Based Lists:
- Insertion and deletion are Θ(n).
- Prev and direct access are Θ(1).
- Array must be allocated in advance.
- No overhead if all array positions are full.
- Linked Lists:
- Insertion and deletion are Θ(1).
- Prev and direct access are Θ(n).
- Space grows with number of elements.
- Every element requires overhead.
30.10.1.21. Space Comparison¶
"Break-even" point:
DE=n(P+E)
n=DEP+E
E: Space for data value.
P: Space for pointer.
D: Number of elements in array.
30.10.1.22. Space Example¶
- Array-based list: Overhead is one pointer (8 bytes) per position in array – whether used or not.
- Linked list: Overhead is two pointers per link node one to the element, one to the next link
- Data is the same for both.
- When is the space the same?
- When the array is half full
30.10.1.23. Freelist¶
System new and garbage collection are slow.
- Add freelist support to the Link class.
30.10.1.24. Doubly Linked Lists¶
null20231215nullheadcurrtail