1 / 20

Introduction to Computer Systems

Introduction to Computer Systems. Department of Computer Science and Information Systems Lecturer: Steve Maybank sjmaybank@dcs.bbk.ac.uk Spring 2019 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 5x5.

rrowlands
Download Presentation

Introduction to Computer Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Review: Arrays • An array is a block of values of the same type • Eg. A 2D array of size 5x5 Birkbeck College, U. London

  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. Birkbeck College, U. London

  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, thenC[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

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

  6. Why are Pointers Useful? • Each pointer contains a relatively small number of bits • Eg. a terabyte memory requires at least 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

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

  8. Example: Alternative List Brookshear, Section 8.2

  9. Lists • A list is a collection of data whose entries are arranged sequentially • Example of a list with 4 entries: The entries may vary widely in size. Brookshear, Sections 8.1 and 8.3

  10. Contiguous Storage of a List • List entries are stored consecutively in memory • Advantage: simple • Disadvantage: insertion and deletion are difficult 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 Brookshear, Section 8.3

  12. Example of a Linked List 7 82 86 87 Head Brookshear, Section 8.3

  13. Pseudocode for Pointers The structures f1 and f2 contain data and a pointer f1.data = 35 # copy data 35 into f1 f1.pointer = f2 # point to f2 f1.data f1.pointer f2.data f2.pointer 35 f2 f2.data f2.pointer Birkbeck College, U. London

  14. Deletion • Find the preceding list item f1 and the following list item f2. Set f1.pointer = f2 deleted entry old pointer f1 f2 new pointer Brookshear, Section 8.3

  15. Insertion • To insert after a list item f1 set newEntry.pointer = f1.pointer f1.pointer = newEntry new pointer newEntry f1 new pointer old pointer Brookshear, Section 8.3

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

  17. Example 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? BB Ch. 8 Review Problems No. 7

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

  19. Binary Tree Each node has the form A B D C Brookshear, Sections 8.3, 8.4

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

More Related