linked lists prof. noah snavely cs1114
Post on 20-Dec-2015
231 views
TRANSCRIPT
![Page 1: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/1.jpg)
Linked lists
Prof. Noah SnavelyCS1114http://cs1114.cs.cornell.edu
![Page 2: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/2.jpg)
Administrivia Assignment 2, Part 2 due tomorrow
– Please don’t wait until the last minute to finish (the last two problems are challenging)
Assignment 3 will be posted tomorrow– Due in two weeks (Friday, 3/6)
Prelim 1 next Thursday, 2/26 in class– Review session: Tuesday or Wednesday evening?– Topics include: running time, graphs, linked lists
2
![Page 3: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/3.jpg)
Making quickselect fast on non-random input
The version of quickselect in A2 can be very slow
What is the problem?
How can we fix it?
3
![Page 4: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/4.jpg)
4
L = getLightColor();if L == ‘red’ robotStop();endif L == ‘green’ robotDriveStraight(r, 10, 100);endif L == ‘yellow’ robotDriveStraight(r, 100, 100);endif L ~= ‘red’ && L ~= ‘green’ && L ~= ‘yellow’ fprintf(‘Unknown light color\n’);end
Conditionals with multiple branches What if we want the robot to correctly
obey a traffic signal?
![Page 5: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/5.jpg)
Conditionals with multiple branches What if we want the robot to correctly
obey a traffic signal?
5
L = getLightColor();if L == ‘red’ robotStop();else if L == ‘green’ robotDriveStraight(r, 10, 100); else if L == ‘yellow’ robotDriveStraight(r, 100, 100); else fprintf(‘Unknown light color\n’); end endend
![Page 6: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/6.jpg)
Conditionals with multiple branches What if we want the robot to correctly
obey a traffic signal?
6
L = getLightColor();if L == ‘red’ robotStop();elseif L == ‘green’ robotDriveStraight(r, 10, 100);elseif L == ‘yellow’ robotDriveStraight(r, 100, 100);else fprintf(‘Unknown light color\n’);end
![Page 7: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/7.jpg)
Last time
Graph traversal
Two types of todo lists:– Stacks Depth-first search– Queues Breadth-first search
7
2
5
1
3
10
6 8
47
92
4
1
5
3
7 9
810
6
![Page 8: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/8.jpg)
Last time
Implementing a stack and queue using arrays
What went wrong?
Today we’ll talk about a better approach
8
![Page 9: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/9.jpg)
9
Linked lists
Alternative to an array
Every element (cell) has two parts:1. A value (as in an array)2. A link to the next cell
![Page 10: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/10.jpg)
10
Linked lists
8 4 1 3
Values
Links
![Page 11: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/11.jpg)
11
Linked lists as memory arrays
We’ll implement linked lists using M
A cell will be represented by a pair of adjacent array entries
M …
![Page 12: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/12.jpg)
12
A few details
I will draw odd numbered entries in blue and even ones in red– Odd entries are values
• Number interpreted as list elements
– Even ones are links• Number interpreted as index of the next cell• AKA location, address, or pointer
The first cell is M(1) and M(2) (for now) The last cell has 0, i.e. pointer to M(0)
– Also called a “null pointer”
![Page 13: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/13.jpg)
13
Example
8 4 1 3
8 5 1
1 2 3
7 4 3
4 5 6
3 0 X
7 8 9
8 3 4
1 2 3
5 1 7
4 5 6
3 0 X
7 8 9
![Page 14: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/14.jpg)
14
Traversing a linked list
Start at the first cell, [M(1),M(2)] Access the first value, M(1) The next cell is at location c = M(2) If c = 0, we’re done Otherwise, access the next value, M(c) The next cell is at location c = M(c+1) Keep going until c = 0
![Page 15: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/15.jpg)
Inserting an element – arrays
How can we insert an element x into an array A?
Depends where it needs to go:– End of the array:
A = [A x];
– Middle of the array (say, between elements A(5) and A(6))?
– Beginning of the array?
15
![Page 16: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/16.jpg)
16
Inserting an element – linked lists
Create a new cell and splice it into the list
Splicing depends on where the cell goes:– How do we insert:
• At the end?• In the middle?• At the beginning?
8 4 1 3
5M(1)
![Page 17: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/17.jpg)
17
Adding a header
We can represent the linked list just by the initial cell, but this is problematic– Problem with inserting at the beginning
Instead, we add a header – a few entries that are not cells, but hold information about the list1.A pointer to the first element2.A count of the number of elements
![Page 18: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/18.jpg)
18
Linked list insertion
Initial list
Insert a 5 at end
Insert an 8 after the 1
5 2 2
1 2 3
0 1 3
4 5 6
X X X
7 8 9
X X X X
10 11 12 13
5 3 2
1 2 3
7 1 3
4 5 6
5 0 X
7 8 9
X X X X
10 11 12 13
1 2 3 4 5 6 7 8 9
5 4 2 7 1 9 5 0 8 3 X X X
10 11 12 13
11 5 2
1 2 3
0 1 9
4 5 6
5 0 8
7 8 9
3 6 5 X
10 11 12 13Insert a 6 at
the start
First element starts at 5 Size of list is 2
![Page 19: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/19.jpg)
Linked list deletion
We can also delete cells
Simply update the header and change one pointers (to skip over the deleted element)
Deleting things is the source of many bugs in computer programs– You need to make sure you delete something
once, and only once
19
![Page 20: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/20.jpg)
20
Linked list deletion
Initial list
Delete the last cell
Delete the 8
5 3 2
1 2 3
0 1 9
4 5 6
5 0 8
7 8 9
3 X X X
10 11 12 13
1 2 3 4 5 6 7 8 9
5 2 2 0 1 3 5 0 8 3 X X X
10 11 12 13
Delete the first cell
1 2 3 4 5 6 7 8 9
5 4 2 7 1 9 5 0 8 3 X X X
10 11 12 13
1 2 3 4 5 6 7 8 9
3 1 2 0 1 3 5 0 8 3 X X X
10 11 12 13
![Page 21: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/21.jpg)
Linked lists – running time
We can insert an item (at the front) in constant (O(1)) time– Just manipulating the pointers– As long as we know where to allocate the cell
We can delete an element (at the front) in constant time
21
![Page 22: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/22.jpg)
Linked lists – running time
What about inserting / deleting from the end of the list?
How can we fix this?
22
![Page 23: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/23.jpg)
23
Doubly linked lists
8 4 1 3
3148
![Page 24: Linked lists Prof. Noah Snavely CS1114](https://reader035.vdocuments.net/reader035/viewer/2022062516/56649d435503460f94a1ff41/html5/thumbnails/24.jpg)
24
A doubly-linked list in memory
4 7 2
1 2 3
0 8 7
4 5 6
4 4 0
7 8 9
48
First element
Last elementSize of list