Introduction to Computer Systems Department of Computer Science

  • Slides: 22
Download presentation
Introduction to Computer Systems Department of Computer Science and Information Systems Lecturer: Steve Maybank

Introduction to Computer Systems Department of Computer Science and Information Systems Lecturer: Steve Maybank sjmaybank@dcs. bbk. ac. uk Spring 2020 Week 7 a: Pointers and Linked Lists 25 February 2020 Birkbeck College, U. London 1

Review: Arrays n n An array is a block of values of the same

Review: Arrays n n An array is a block of values of the same type Eg. A 2 D array B of size 3 x 3 65 -6 8 77 32 23 23 -5 0 B Birkbeck College, U. London 2

Review: Array Indexing n n n There is a standard way of referring to

Review: Array Indexing n n n There is a standard way of referring to the entries in an array. Eg. in Python: A[0], A[9], B[12, 1]. In 2 D arrays, the order of the indices is row then column. Birkbeck College, U. London 3

Three Dimensional Arrays § If C is a 3 D array of size 10

Three Dimensional Arrays § If C is a 3 D array of size 10 x 10, if each entry occupies one memory cell and if C[0, 0, 0] is stored at x, then C[i, j, k] is stored at location x+100*i+10*j+k § For example C[2, 5, 1] is stored at location x+100*2+10*5+1 = x+251 Birkbeck College, U. London 4

Worksheet for Week 6, Q 1 § An array with 6 rows and 8

Worksheet for Week 6, Q 1 § An array with 6 rows and 8 columns is stored in row major order, starting at decimal address 50. Each entry requires two memory cells. What is the address of the entry in the 5 th row and 7 th column? Brookshear Ch 8 Review Problems Q 2 5

Worksheet for Week 6, Q 2 § Write pseudo code such that if an

Worksheet for Week 6, Q 2 § Write pseudo code such that if an element in an mxn matrix is 0, then all elements in the same row or in the same column are set to zero Brookshear Ch 8 Review Problems Q 4 6

Pointers § A pointer is a storage area containing the address at which a

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 programme counter 207 60 The programme counter points to memory cell 207 which contains the value 60 Brookshear, Sections 8. 2 and 8. 7 7

Why are Pointers Useful? n n n Each pointer contains a relatively small number

Why are Pointers Useful? n n n Each pointer contains a relatively small number of bits Eg. a terabyte memory requires about 40 bits in a pointer It is easier to move or copy pointers rather than move or copy the data they point to Brookshear, Section 8. 2 8

Example: Sorting 67 84 A Z 92 M 67 84 92 67 A Array

Example: Sorting 67 84 A Z 92 M 67 84 92 67 A Array of pointers 84 Z Data in memory 92 M 67 92 84 Data in memory Sorted array of pointers Birkbeck College, U. London 9

Example: Alternative List A Farewell to Arms Hemingway ------… Pointer For Whom the Bell

Example: Alternative List A Farewell to Arms Hemingway ------… Pointer For Whom the Bell Tolls The Sun Also Rises Hemingway --------… … … ------Pointer Brookshear, Section 8. 2 10

Lists n n A list is a collection of data whose entries are arranged

Lists n n 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 may vary widely in size. Brookshear, Sections 8. 1 and 8. 3 11

Contiguous Storage of a List n List entries are stored consecutively in memory n

Contiguous Storage of a List n List entries are stored consecutively in memory n Advantage: simple n Disadvantage: insertion and deletion are difficult Brookshear, Section 8. 3 12

Linked Lists § Each list entry contains a pointer to the next entry §

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 Brookshear, Section 8. 3 13

Example of a Linked List 7 photo 86 82 address 7 86 date 87

Example of a Linked List 7 photo 86 82 address 7 86 date 87 Brookshear, Section 8. 3 87 tel Head Nil 82 14

Pseudocode for Pointers The structures f 1 and f 2 contain data and a

Pseudocode for Pointers The structures f 1 and f 2 contain data and a pointer f 1. data f 1. pointer f 2. data f 2. pointer f 1. data = 35 # copy data 35 into f 1. pointer = f 2 # point to f 2 35 f 2. data f 2. pointer Birkbeck College, U. London 15

Deletion § Find the preceding list item f 1 and the following list item

Deletion § Find the preceding list item f 1 and the following list item f 2. Set f 1. pointer = f 2 old pointer f 1 data deleted entry data pointer new pointer Brookshear, Section 8. 3 data f 2 pointer 16

Insertion § To insert after a list item f 1 set new. Entry. pointer

Insertion § To insert after a list item f 1 set new. Entry. pointer = f 1. pointer = new. Entry f 1 data pointer new. Entry data pointer new pointer old pointer Brookshear, Section 8. 3 data pointer 17

Printing a Linked List f = head(L) while (f<>nil) print(f. data) f = f.

Printing a Linked List f = head(L) while (f<>nil) print(f. data) f = f. next end. While Birkbeck College, U. London 18

Example address contents 11 12 13 14 15 16 17 18 19 20 21

Example address contents 11 12 13 14 15 16 17 18 19 20 21 22 C G E B U F The table represents the contents of some cells in memory, along with the address of each cell. Place addresses in the empty cells such that each cell containing a letter together with the following cell form an entry in a linked list in which the letters appear in alphabetical order. What address should the head pointer contain? BB Ch. 8 Review Problems No. 7 19

Example of a Tree S N S: sentence NP: noun phrase VP: verb phrase

Example of a Tree S N S: sentence NP: noun phrase VP: verb phrase N: noun V: verb D: determiner Colour: data Tree: hidden structure VP John V hit NP D N the ball http: //en. wikipedia. org/wiki/Parse_tree Birkbeck College, U. London 20

Binary Tree A B C D Each node has the form data left pointer

Binary Tree A B C D Each node has the form data left pointer Brookshear, Sections 8. 3, 8. 4 right pointer 21

Binary Tree Stored in Memory 29 30 31 32 33 34 35 36 37

Binary Tree Stored in Memory 29 30 31 32 33 34 35 36 37 38 A 33 39 B 36 nil C nil 39 40 41 42 43 D nil The head pointer has the value 30 Birkbeck College, U. London 22