+ All Categories
Home > Documents > 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve...

10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve...

Date post: 18-Jan-2016
Category:
Upload: whitney-cox
View: 212 times
Download: 0 times
Share this document with a friend
20
10 November 2015 Birkbeck College, U. London 1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] Autumn 2015 Week 7a: Pointers and Linked Lists
Transcript
Page 1: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

10 November 2015 Birkbeck College, U. London 1

Introduction to Computer Systems

Lecturer: Steve Maybank

Department of Computer Science and Information [email protected]

Autumn 2015

Week 7a: Pointers and Linked Lists

Page 2: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 3: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 4: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 5: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 6: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

Page 7: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 8: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 9: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

Page 10: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

Page 11: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

Page 12: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 13: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 14: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 15: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 16: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 17: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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?

Page 18: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 19: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Page 20: 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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


Recommended