Your tutor has asked a lab pair to present their week 7 work.
Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!
Try out the reference Castle Defense implementation: 1511 castle_defense.
Do you have questions that others in the tutorial may be able to answer?
struct overall {
struct node *start;
struct node *end;
};
struct node {
int data;
struct node *next;
};
where start
points at the first element of the list and
end
points at the last element of the list:
overall
struct look like in memory?
What is its purpose?
node
struct look like in memory? What
is its purpose?
malloc
function do? How could you use
it to allocate memory for a single struct node
?
new_node
that creates a new
node with the specified data value:
struct node *new_node(int data) {
}
insert_at_head
that uses your
new_node
function to create a node, and then inserts
that node at the front of the linked list.
void insert_at_front(struct overall *o, int data) {
}
insert_before_end
that
inserts an element just before the end element.
void insert_before_end(struct overall *o, int data) {
}
#define MAX_SPECIES_NAME_LENGTH 128 struct pod { struct date when; int how_many; char species[MAX_SPECIES_NAME_LENGTH]; };What if we use a linked list of this struct to store a whale sighting?
struct pod { struct pod *next; struct date *when; int how_many; char *species; };And a date is still represented by the same struct:
struct date { int year; int month; int day; };Sketch out how these two data structures would be laid out in memory.
Discuss the differences and how that will affect the use of this data
Your tutor may still choose to cover some of the questions time permitting.
split_string
which takes a string
read by fgets containing substrings separated by commas, places
the substrings in an char[][] array and returns the number of
strings.
You can assume there are at most 128 words on the line and no word is more than 32 characters long.
print_substrings
which prints one
per line the substrings in the char[][] array created in the
previous question.
substrings_sorted
which given the
char[][] array in the previous question returns 1 if the
substrings are increasing order and 0 otherwise.
search_substrings
which given the
char[][] array in the previous question returns 1 if the array
contains a specified string and 0 otherwise.
substring.c
which reads lines using
fgets and calls the above functions.
struct date
pointers
and compares returns 0 if they are equal, -1 if the first is less than
the second and 1 if the first is larger than the second.
int compare_date(struct date *d1, struct date *d2);