Week 5 - CS50cdn.cs50.net/2016/fall/lectures/5/week5.pdf · typedef struct { int front; int...
Transcript of Week 5 - CS50cdn.cs50.net/2016/fall/lectures/5/week5.pdf · typedef struct { int front; int...
Week 5
last time
environment variables
stack
uninitialized data
initialized data
text
heap
voidswap(inta,intb){inttmp=a;a=b;b=tmp;}
voidswap(int*a,int*b){inttmp=*a;*a=*b;*b=tmp;}
typedefstruct{stringname;stringdorm;}student;
this time
https://cs.calvin.edu/activities/books/c++/ds/1e/
http://cs.calvin.edu/books/c++/ds/1e/
typedefstructnode{intn;structnode*next;}node;
delete
insert
search
...
https://cs.calvin.edu/activities/books/c++/ds/1e/
insert at tail
https://cs.calvin.edu/activities/books/c++/ds/1e/
insert at head
https://cs.calvin.edu/activities/books/c++/ds/1e/
insert in middle
https://cs.calvin.edu/activities/books/c++/ds/1e/
insert in middle
https://cs.calvin.edu/activities/books/c++/ds/1e/
insert in middle
https://cs.calvin.edu/activities/books/c++/ds/1e/
remove tail
https://cs.calvin.edu/activities/books/c++/ds/1e/
remove head
https://cs.calvin.edu/activities/books/c++/ds/1e/
remove in middle
https://cs.calvin.edu/activities/books/c++/ds/1e/
delete
insert
search
...
boolsearch(intn,node*list){node*ptr=list;while(ptr!=NULL){if(ptr->n==n){returntrue;}ptr=ptr->next;}returnfalse;}
push
pop
...
typedefstruct{intnumbers[CAPACITY];intsize;}stack;
typedefstruct{int*numbers;intsize;}stack;
enqueue
dequeue
...
typedefstruct{intfront;intnumbers[CAPACITY];intsize;}queue;
typedefstruct{intfront;int*numbers;intsize;}queue;
environment variables
stack
uninitialized data
initialized data
text
heap
22 33 44 55 66 77 88
22 33 44 55 66 77 88
Figure from http://cs.calvin.edu/books/c++/ds/1e/.
binary search tree
typedefstructnode{intn;structnode*left;structnode*right;}node;
boolsearch(intn,node*tree){if(tree==NULL){returnfalse;}elseif(n<tree->n){returnsearch(n,tree->left);}elseif(n>tree->n){returnsearch(n,tree->right);}else{returntrue;}}
ASCII
A B C D E F G H I ...65 66 67 68 69 70 71 72 73 ...
Figure by Larry Nyhoff.
Figure by Larry Nyhoff.
Figure by Larry Nyhoff.
Figure by Larry Nyhoff.
Figure by Larry Nyhoff.
A is 01
B is 0000
C is 0001
D is 001
E is 1
typedefstructnode{charsymbol;floatfrequency;structnode*left;structnode*right;}node;
... O(n) O(log n) O(1) …
Figure from Lewis and Denenberg’s Data Structures & Their Algorithms.
typedefstructnode{boolword;structnode*children[27];}node;
Week 5