10 november 2015birkbeck college, u. london1 introduction to computer systems lecturer: steve...
Post on 18-Jan-2016
213 Views
Preview:
TRANSCRIPT
10 November 2015 Birkbeck College, U. London 1
Introduction to Computer Systems
Lecturer: Steve Maybank
Department of Computer Science and Information Systemssjmaybank@dcs.bbk.ac.uk
Autumn 2015
Week 7a: Pointers and Linked Lists
Review: Arrays
An array is a block of values of the same type.
Eg. A 2D array of size 10x10.
10 November 2015 Birkbeck College, U. London 2
Review: Array Indexing There is a standard way of
referring to the entries in an array.
Eg. in Python: A[0], A[9], B[12,1].
In 2D arrays, the order of the indices is row then column.
10 November 2015 Birkbeck College, U. London 3
Three Dimensional Arrays If C is a 3D array of size 10x10x10,
if each entry occupies one memory cell and if C[0,0,0] is stored at x, thenC[i, j, k] is stored atx+100*i+10*j+kThus C[2,5,1] is stored atx+100*2+10*5+1 = x+251
10 November 2015 Birkbeck College, U. London 4
10 November 2015 Brookshear, Sections 8.2 and 8.7 5
Pointers A pointer is a storage area
containing the address at which a piece of information is stored.
Example: the program counter in the CPU.
207 60
207
The programme counter points to memory cell 207 whichcontains the value 60.
programme counter
10 November 2015 Brookshear, Section 8.2 6
Why are Pointers Useful?
Each pointer contains a relatively small number of bits.
It is easier to move or copy pointers rather than move or copy the data they point to.
10 November 2015 Birkbeck College, U. London 7
Example: Sorting
A Z M
67 84
92
A Z M
67 92 84
67 84 92
Array of pointers
Data in memory
67 84 92
Data in memory
Sorted array of pointers
10 November 2015 Brookshear, Section 8.2 8
Example: Alternative List
…
A Farewell to Arms Hemingway------------Pointer
…
For Whom the Bell Tolls Hemingway------------Pointer
…
The Sun Also Rises Hemingway-----------------Pointer
…
10 November 2015 Brookshear, Sections 8.1 and 8.3 9
Lists A list is a collection of data whose
entries are arranged sequentially. Example of a list with 4 entries:
address
photo date tel.
The entries vary widely in size.
10 November 2015 Brookshear, Section 8.3 10
Contiguous Storage of a List
List entries are stored consecutively in memory
Advantage: simple Disadvantage: insertion and
deletion are difficult.
10 November 2015 Brookshear, Section 8.3 11
Linked Lists
Each list entry contains a pointer to the next entry
List entries can be stored in any part of memory.
There is a special head pointer for the first entry and a Nil pointer in the last entry.
10 November 2015 Brookshear, Section 8.3 12
Example of a Linked List
photo 86 address
7 date
87
tel Nil 827 82 86 87 Head
10 November 2015 Brookshear, Section 8.3 13
Deletion
Find the preceding list item f1 and the following list item f2. Set f1.pointer=f2
data
pointer
data
pointer
data
pointer
f1
f2
deleted entry
new pointer
oldpointer
10 November 2015 Brookshear, Section 8.3 14
Insertion
to insert after a list item f1 set newEntry.pointer = f1.pointer
f1.pointer = location of newEntry
data
pointer
data
pointer
data
pointer
new pointer
old pointer
f1 newEntrynewpointer
10 November 2015 Birkbeck College, U. London 15
Pseudocode for Pointers
Assign the value 4 to x:f1.x = 4
Point to the next item:f1.next = f2
pointer f1 variable x pointer next
10 November 2015 Birkbeck College, U. London 16
Printing a Linked List
f = head(L);While (f<>nil)
print(f.data);f = f.next;
EndWhile
10 November 2015 BB Ch. 8 Review Problems No. 7 17
Example
11 C12 13 G14 15 E16 17 B18 19 U20 21 F22
address contents
The table represents the contents ofsome cells in memory, along with theaddress of each cell. Place addressesin the empty cells such that each cellcontaining a letter together with thefollowing cell form an entry in alinked list in which the letters appearin alphabetical order.
What address should the head pointercontain?
Example of a Tree
10 November 2015 Birkbeck College, U. London 18
S
N VP
V NPJohn
hit D N
the ball
http://en.wikipedia.org/wiki/Parse_tree
S: sentenceNP: noun phraseVP: verb phraseN: nounV: verbD: determinerColour: dataTree: hidden structure
Binary Tree
Each node has the form
10 November 2015 Brookshear, Sections 8.3, 8.4 19
data left pointer right pointer
A
B
C
D
Binary Tree Stored in Memory
A 33
39
B 36
nil C nil nil D nil nil
10 November 2015 Birkbeck College, U. London 20
30 36 39383729 3534333231 40 41 42 43
top related