COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s...

42
COMP40 More on Abstract Data Types

Transcript of COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s...

Page 1: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

COMP40More on Abstract Data Types

Page 2: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 3: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 4: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

This generalization is known as universal polymorphism

Page 5: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

List_push(List_T list, void *v)

void *v = s;

void *v = c;

void *v = p;

void *v = l;

Page 6: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 7: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

/* Declare the type Table_T to be a struct pointer */typedef struct T *T;

/* Define Table_new to return the struct pointer */T Table_new(...);

#define T Table_T

Page 8: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

struct Car {char *brand; int year;}; typedef struct Car Car;

List_T mylist = List_list(NULL); Car *retrieved_car;Car mycar = {"ford", 2000};

mylist = List_push(mylist, &mycar); mylist = List_pop(mylist, (void **)&retrieved_car);

List_free(&mylist);

Page 9: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 10: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void double(int n) {n = n * 2;

}

int x = 3;double(x);printf(“x = %d\n”, x);

void double(int *n) {*n = *n * 2;

}

int x = 3;double(&x);printf(“x = %d\n”, x);

Page 11: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}

/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 12: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}

my_string

length = ?

sp

/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 13: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}

my_string

length = ?

sp

/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 14: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}

my_string

length = ?

sp

h e l l o \0

/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 15: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}

my_string

length = ?

sp

h e l l o \0

length = 5

my_string/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 16: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string( &my_string); printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char **sp_p) { *sp_p = malloc(...); /* Set **sp_p, *(*sp_p + 1), … */ return strlen(*sp_p);}

my_string

length = ?

sp_p

/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 17: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int main(){ char *my_string; int length = get_string( &my_string); printf(“We have string %s of size %d”, my_string, length);

}

int get_string(char **sp_p) { *sp_p = malloc(...); /* Set **sp_p, *(*sp_p + 1), … */ return strlen(*sp_p);}

my_string

length = ?

sp_p

h e l l o \0

length = 5

/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */

Page 18: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 19: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void print_hi() {printf(“%s\n”, “hi”);

}void print_no() {

printf(“%s\n”, “no”);}void print_yes() {

printf(“%s\n”, “yes”);}

void print_string(char *s) {printf(“%s\n”, s);

}

Page 20: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void List_push_int(List_int *l, int x) { /* Add in new integer element */}void List_push_char(List_char *l, char x) { /* Add in new character element */}

void List_push(List_T *l, void *x) { /* Add in new element */}

Page 21: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int find_min(int[] nums, int size) { int min = nums[0]; for (int i = 1; i < size; i++) { if (min > nums[i]) min = nums[i]; } return min;}

int find_max(int[] nums, int size) { int max = nums[0]; for (int i = 1; i < size; i++) { if (max < nums[i]) max = nums[i]; } return max;}

Page 22: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

T f(int[] nums, int size, <func>) { T cl; for (int i = 0; i < size; i++) { cl = <func>(nums[i], cl); } return cl;}

void f(int[] nums, int size, <func>) { for (int i = 0; i < size; i++) { <func>(nums[i]); } }

Page 23: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int x = 77;int *p = &x;

void f() {printf(“Hello”);

} f<func> *fp;fp = f; pfp();

Page 24: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int f(int x); //function declaration (prototype)

int *g(int x); //function returning an int *int (*h)(int x); //function pointer declaration

int f(int x) {return x + 1;

}

int main() {//& and * are optionalh = &f;printf(“%d\n”, (*h)(4));

}

Page 25: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int f(int x); //function declaration (prototype)

int *g(int x); //function returning an int *int (*h)(int x); //function pointer declaration

int f(int x) {return x + 1;

}

int main() {//& and * are optionalh = f;printf(“%d\n”, h(4));

}

Page 26: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int cube(int n) { return n * n * n;}int func_app(int n, int f(int x)) { return f(n);}int main() { int (*func_ptr)(int x); //parameters may be named func_ptr = cube; printf(“%d\n”, cube(4)); printf(“%d\n”, func_ptr(4)); printf(“%d\n”, func_app(4, func_ptr));}

Page 27: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 28: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

int digits[30];int i;

for (i = 0; i < 30; i++) { digits[i] = i;}

for (i = 0; i < 30; i++) { digits[i] = f(i);}

for (i = 0; i < 30; i += 2) { digits[i] = g(i);}

Page 29: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { //perform computation on *x}

List_T list = List_list(...);

//populate the list

List_map(list, f, NULL);

Page 30: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { (void) cl; int *elem = *x; *elem += 10;}

int *digits = malloc(3 * sizeof(*digits));

for (int i = 0; i < 3; i++) {*(digits + i) = i;

}List_T list = List_list( digits,

(void *)(digits+1), (void *)(digits+2), NULL);

List_map(list, f, NULL);

Page 31: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}

int *digits = malloc(3 * sizeof(*digits));int accum = 0;for (int i = 0; i < 3; i++) {

*(digits + i) = i;}List_T list = List_list( digits,

(void *)(digits+1), (void *)(digits+2), NULL);

List_map(list, f, &accum);

Page 32: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

0 1 2

0

Page 33: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

10 1 2

10

Page 34: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

10 1 2

10

Page 35: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

10 11 2

21

Page 36: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

10 11 2

21

Page 37: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

10 11 12

33

Page 38: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);

10 11 12

33

Page 39: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}

int *digits = malloc(3 * sizeof(*digits));int accum = 0;for (int i = 0; i < 3; i++) {

*(digits + i) = i;}List_T list = List_list( digits,

(void *)(digits+1), (void *)(digits+2), NULL);

List_map(list, f, &accum);

Page 40: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...
Page 41: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

10 11 12

1011

12

Page 42: COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);} int get_string(char *sp) {sp = malloc ...

10 11 12

1011

12