Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c8dbda2

Browse files
added notes for 3.1 arrays and linked lists
1 parent 9cf0f7c commit c8dbda2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
layout: post
3+
title: 3.1 Arrays and linked lists
4+
---
5+
6+
* **Contiguously-allocated structures** are composed of single slabs of memory
7+
* **Linked data structures** are composed of distinct chunks of memory bound together by pointers
8+
9+
## Arrays
10+
11+
* fundamental contiguously-allocated data structure
12+
* arrays are structures of fixed-size data records
13+
* each element can be efficiently located by its index
14+
* constant-time access given the index
15+
* space efficient as no links/pointers
16+
* memory locality: continuity between successive data accesses helps in exploiting CPU/memory caches
17+
* cannot adjust their size in the middle of a program’s execution
18+
19+
### Dynamic arrays
20+
21+
When running out of allocated space:
22+
23+
* allocate bigger (e.g double) space
24+
* and copy old contents
25+
26+
How many times might an element have to be recopied after a total of n insertions?
27+
28+
* starting from log2n doublings to get to n elements
29+
* last half will only be copied once
30+
* a quarter of the elements will be copied twice
31+
* all movements M will always be less than 2n
32+
33+
![image](images/3.1-dynamic_array.jpg)
34+
35+
* each of the n elements move only two times on average
36+
37+
* total work of managing the dynamic array is the same O(n)
38+
* no longer constant access all the time
39+
40+
## Pointers and Linked Structures
41+
42+
* pointers represent the address of a location in memory
43+
* each node in our data structure contains one or more data fields that retain the data that we need to store
44+
* each node contains a pointer field to at least one other node (e.g. next)
45+
* (please note all these pointers require extra space)
46+
* we also need a pointer to the head of the structure, so we know where to access it
47+
48+
### Linked lists
49+
50+
* simplest linked structure
51+
* special linked list: **doubly-linked**, each node points both to its predecessor and its successor element (some operations are simpler at the cost of the extra pointer)
52+
* searching: if x is in the list, it is either the first element or located in the smaller rest of the list
53+
* insertion: simplest, insert at the beginning and update the head of the list
54+
* deletion: recurively find the precedessor and update its 'next' pointer (take care when head is deleted or the list doesn't contain the element)
55+
56+
## Comparison
57+
58+
The relative advantages of linked lists:
59+
60+
* overflow on linked structures only when memory is really full
61+
* insertions and deletions are simpler than for contiguous (array) lists.
62+
* with large records it's easier to move data then pointers
63+
64+
The relative advantages of arrays include:
65+
66+
* no extra space required for pointers
67+
* efficient random access
68+
* better memory locality and cache performance than random pointer jumping
69+
70+

‎images/3.1-dynamic_array.jpg

3.46 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /