Introduction To Computer Science I Harvard College
Introduction To Computer Science I Harvard College
Comput er Sc i enc e 50
Introduction to Computer Science I
Har var d Col l ege
David J. Malan
[email protected]
Week 6
1
CS 50s Library
Revisited
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 Lists
typedef struct _node
{
int n;
struct _node *next;
}
node;
see
list1.{c,h}
3
Singly Linked Lists
see
list2.{c,h}
typedef struct
{
int id;
char * name;
char * house;
}
student;
typedef struct _node
{
student * student;
struct _node *next;
}
node;
4
Singly Linked Lists
Representation
9 17 22 26 34
first
n
next
Figure adapted from http://cs.calvin.edu/books/c++/ds/1e/.
5
Singly Linked Lists
Traversal
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
9 17 22 26 34
first
ptr
9 17 22 26 34
first
ptr
.
.
.
9 17 22 26 34
first
ptr
9 17 22 26 34
first
ptr
6
Singly Linked Lists
Insertion 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 Lists
Insertion 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 Lists
Insertion 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 Lists
Insertion at Tail
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
9 17 22 29 34
first
20
55
predptr
newptr
10
Singly Linked Lists
Insertion at Head
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
9 17 22 29
55
first 20
5
predptr
newptr
34
11
Singly Linked Lists
Deletion from Middle
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
5 17 22 29 34
first
20 9
predptr
ptr
free
12
Singly Linked Lists
Deletion from Tail
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
5 17 22 29
first
9
predptr
ptr
34
free
13
Singly Linked Lists
Deletion from Head
Figure adapted from http://cs.calvin.edu/books/c++/ds/1e/.
14
Doubly Linked Lists
Representation
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
L
first
mySize
5
last
9 17 22 26 34
prev
next
15
Hash Tables
Linear Probing
16
Hash Tables
The Birthday Problem
In a room of n CS 50 students,
whats the probability that at least
two students share the same birthday?
17
Hash Tables
The Birthday Problem
Image from http://en.wikipedia.org/wiki/Birthday_paradox.
18
Hash Tables
The Birthday Problem
Image from http://www.mste.uiuc.edu/reese/birthday/probchart.GIF.
19
Hash Tables
Coalesced Chaining
Figure from Lewis and Denenbergs Data Structures & Their Algorithms.
20
Hash Tables
Separate Chaining
Figure from Lewis and Denenbergs Data Structures & Their Algorithms.
21
Comput er Sc i enc e 50
Introduction to Computer Science I
Har var d Col l ege
David J. Malan
[email protected]
Week 6