Things we've covered this year
- command line interface to compile c code:
>>> gcc family-tree.c -o family-tree
- writing header files and breaking up c into multiple linked files. Example header file that includes definitions for each sort function:
void selectionSort(int arr[], int n);
void insertionSort(int arr[], int n);
void bubbleSort(int arr[], int n);
- big O notation and sorting functions; selection, bubble, and insertion are all O(n2) while merge, quick and heap are are typically O(nlogn) or similar because they break the problem in half at each computation
- Defining structures and creating types:
typedef struct _node *node;
struct _node{
char name[20];
int child_amount;
node mother;
node father;
node spouse;
node *children;
};
- Allocating memory properly when declaring a structure. IE: if you're declaring a pointer to a block of memory you first have to allocate that block of memory.
node newNode(char *title){
node person = malloc(sizeof(struct _node));
person->children = malloc(sizeof(node) * 10);
strncpy(person->name, title, 20);
person->mother = NULL;
person->father = NULL;
person->spouse = NULL;
int child_amount = 0;
return person;
};
- Above works, below doesnt:
node newNode(char *title){
node person;
strncpy(person->name, title, 20);
person->mother = NULL;
person->father = NULL;
person->spouse = NULL;
int child_amount = 0;
return person;
};
- Passing pointers properly - for instance if you are passing arguments to a function that takes pointers, you can pass a pointer, or you can pass the address of a variable with &var
- setting up linked lists and trees with structures that contain pointers to the next structure
- Navigating trees using recursion
Using Recursion
- Implemented a depth-first search with whitespace adding
- printout is little off, not quite sure why but it mostly works, here's the output:
>>> ./family-tree
greg Spouse: laurie
nate Spouse: wifey
child1
childOfChild1
childOfChildOfChild1
child2
caitlin
- caitlin should line up with nate and child1 should line up with child2... it also crashes periodically and infinite loops spaces... but works 4 out of 5 times and prints that.