Feb 17
The code I did in class is attached.
Start a discussion of data structures and their implementation in C with pointers and structures.
These concepts are crucial in a whole slew of algorithms and their implementations;
the point here is to start to see (a) the basic ideas, and (b) to start to get
a feel for the low-level (i.e. C) implementations. Higher level languages (like Python)
use these (and other tricks) behind the scenes.
some sources :
and
Use these ideas to implement and discuss data structures, for
example linked lists, binary trees, and general graphs (algthough there are other ways to store graphs, too.)
Recursive algorithms are excellent for dealing with these sort of structures,
since each piece is another one of them.
Look at examples from my online code, for example the N Queens problem :
Part of this week's assignment will be to download, compile, run, and
look at this nqueens code; I think it's a good example of these
structs, pointers, etc.
This is the basic model:
#include <stdlib.h>
#include <stdio.h>
typedef struct _foo *foo; // define a "foo" type as a pointer to "struct _foo"
struct _foo {
int value; // various fields within the structure
char* name;
foo next; // pointer to another one (recursion, anyone?)
};
foo new_foo(int its_value, char* its_name){
foo the_foo = (foo) malloc(sizeof(struct _foo)); // get pointer to new memory
the_foo->value = its_value; // fill in pieces
the_foo->name = its_name;
the_foo->next = NULL; // This is how you do an empty pointer.
return the_foo;
}
int main(){
foo f = new_foo(17, "charlie"); // create one named "f"
printf("fs (value, name) = (%i, %s)\n", f->value, (*f).name); // x->;y same as (*x).y
free(f); // Explicit memory management - important in subroutines and daemons.
return 0;
}
Test your C chops:
What exactly are these and where might you use them?
1: char *foo(int** x)
2: char (*foo)(int** x)