22
0 Computer Science 50 Introduction to Computer Science I Harvard College David J. Malan [email protected] Week 6

Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

0

Computer Science 50Introduction to Computer Science I

Harvard College

David J. [email protected]

Week 6

Page 2: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

1

CS 50’s LibraryRevisited

see~cs50/pub/releases/cs50/cs50.{c,h}

bool

string

char GetChar();

double GetDouble();

float GetFloat();

int GetInt();

long long GetLongLong();

string GetString();

Page 3: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

2

Singly Linked Liststypedef struct _node

{

int n;

struct _node *next;

}

node;

seelist1.{c,h}

Page 4: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

3

Singly Linked Lists

seelist2.{c,h}

typedef struct

{

int id;

char * name;

char * house;

}

student;

typedef struct _node

{

student * student;

struct _node *next;

}

node;

Page 5: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

4

Singly Linked ListsRepresentation

9 17 22 26 34firstn

next

Figure adapted from http://cs.calvin.edu/books/c++/ds/1e/.

Page 6: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

5

Singly Linked ListsTraversal

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

9 17 22 26 34first

ptr

9 17 22 26 34first

ptr

. . .

9 17 22 26 34first

ptr

9 17 22 26 34first

ptr

Page 7: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

6

Singly Linked ListsInsertion in Middle: Step 1

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

20

9 17 22 29 34

predptr

newptr

first

Page 8: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

7

Singly Linked ListsInsertion in Middle: Step 2

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

20

9 17 22 29 34

predptr

newptr

first

Page 9: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

8

Singly Linked ListsInsertion in Middle: Step 3

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

20

9 17 22 29 34

predptr

newptr

first

Page 10: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

9

Singly Linked ListsInsertion at Tail

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

9 17 22 29 34first 20

55

predptr

newptr

Page 11: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

10

Singly Linked ListsInsertion at Head

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

9 17 22 29 55first 20

5

predptr

newptr

34

Page 12: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

11

Singly Linked ListsDeletion from Middle

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

5 17 22 29 34first 209

predptr ptrfree

Page 13: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

12

Singly Linked ListsDeletion from Tail

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

5 17 22 29first 9

predptr ptr

34

free

Page 14: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

13

Singly Linked ListsDeletion from Head

Figure adapted from http://cs.calvin.edu/books/c++/ds/1e/.

Page 15: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

14

Doubly Linked ListsRepresentation

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

L

first

mySize 5

last9 17 22 26 34

prev

next

Page 16: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

15

Hash TablesLinear Probing

Page 17: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

16

Hash TablesThe Birthday Problem

In a room of n CS 50 students, what’s the probability that at least

two students share the same birthday?

Page 18: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

17

Hash TablesThe Birthday Problem

Image from http://en.wikipedia.org/wiki/Birthday_paradox.

Page 19: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

18

Hash TablesThe Birthday Problem

Image from http://www.mste.uiuc.edu/reese/birthday/probchart.GIF.

Page 20: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

19

Hash TablesCoalesced Chaining

Figure from Lewis and Denenberg’s Data Structures & Their Algorithms.

Page 21: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

20

Hash TablesSeparate Chaining

Figure from Lewis and Denenberg’s Data Structures & Their Algorithms.

Page 22: Week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · Week 6. Title: Microsoft PowerPoint - week6.ppt Created Date: 11/5/2007 6:48:45 PM

21

Computer Science 50Introduction to Computer Science I

Harvard College

David J. [email protected]

Week 6