week 6 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/6/week6.pdf · week 6. title: microsoft...

Post on 28-Aug-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

0

Computer Science 50Introduction to Computer Science I

Harvard College

David J. Malanmalan@post.harvard.edu

Week 6

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();

2

Singly Linked Liststypedef struct _node

{

int n;

struct _node *next;

}

node;

seelist1.{c,h}

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;

4

Singly Linked ListsRepresentation

9 17 22 26 34firstn

next

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

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

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

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

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

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

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

11

Singly Linked ListsDeletion from Middle

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

5 17 22 29 34first 209

predptr ptrfree

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

13

Singly Linked ListsDeletion from Head

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

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

15

Hash TablesLinear Probing

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?

17

Hash TablesThe Birthday Problem

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

18

Hash TablesThe Birthday Problem

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

19

Hash TablesCoalesced Chaining

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

20

Hash TablesSeparate Chaining

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

21

Computer Science 50Introduction to Computer Science I

Harvard College

David J. Malanmalan@post.harvard.edu

Week 6

top related