Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 10 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2015 Week 7a: Pointers and Linked Lists

2 Review: Arrays An array is a block of values of the same type. Eg. A 2D array of size 10x10. 10 November 2015Birkbeck College, U. London2

3 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 2015Birkbeck College, U. London3

4 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, then C[i, j, k] is stored at x+100*i+10*j+k Thus C[2,5,1] is stored at x+100*2+10*5+1 = x+251 10 November 2015Birkbeck College, U. London4

5 10 November 2015Brookshear, Sections 8.2 and 8.75 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 which contains the value 60. programme counter

6 10 November 2015Brookshear, Section 8.26 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.

7 10 November 2015Birkbeck College, U. London7 Example: Sorting AZM 678492 AZM 679284 678492 Array of pointers Data in memory 678492 Data in memory Sorted array of pointers

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

9 10 November 2015Brookshear, Sections 8.1 and 8.39 Lists A list is a collection of data whose entries are arranged sequentially. Example of a list with 4 entries: addressphotodatetel. The entries vary widely in size.

10 10 November 2015Brookshear, Section 8.310 Contiguous Storage of a List List entries are stored consecutively in memory Advantage: simple Disadvantage: insertion and deletion are difficult.

11 10 November 2015Brookshear, Section 8.311 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.

12 10 November 2015Brookshear, Section 8.312 Example of a Linked List photo86address7date87telNil82 7 8687Head

13 10 November 2015Brookshear, Section 8.313 Deletion  Find the preceding list item f1 and the following list item f2. Set f1.pointer=f2 datapointer datapointer datapointer f1 f2 deleted entry new pointer old pointer

14 10 November 2015Brookshear, Section 8.314 Insertion  to insert after a list item f1 set newEntry.pointer = f1.pointer f1.pointer = location of newEntry datapointer datapointer datapointer new pointer old pointer f1 newEntrynew pointer

15 10 November 2015Birkbeck College, U. London15 Pseudocode for Pointers Assign the value 4 to x: f1.x = 4 Point to the next item: f1.next = f2 pointer f1 variable xpointer next

16 10 November 2015Birkbeck College, U. London16 Printing a Linked List f = head(L); While (f<>nil) print(f.data); f = f.next; EndWhile

17 10 November 2015BB Ch. 8 Review Problems No. 717 Example 11C 12 13G 14 15E 16 17B 18 19U 20 21F 22 address contents 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?

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

19 Binary Tree Each node has the form 10 November 2015Brookshear, Sections 8.3, 8.419 dataleft pointerright pointer A B C D

20 Binary Tree Stored in Memory A3339B36nilC D 10 November 2015Birkbeck College, U. London20 3036 39 3837 29 35 34 33 32 31 40 41 42 43


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

Similar presentations


Ads by Google