These questions have not been updated this term. They may be useful for revision,
but will not be used as part of teaching.
Malloc
-
What is
malloc
?
-
What is
sizeof
? What does it give us?
-
Create an
int *
variable that points to memory allocated using
malloc
.
-
For this question, the following struct is defined:
struct student {
int zID;
double mark;
char name[MAX_NAME_LENGTH];
};
Write a function struct student *create_student(int zID, double mark, char *name)
that creates a new struct student
and returns a pointer to it.
Linked Lists
The following questions will make use of the struct student
defined
in the malloc section as well as the create_student()
function.
-
What is a linked list? How does it relate to
struct
in C?
-
How can we modify
struct student
to contain a pointer to another
struct student
?
-
Why does this new member have to be a pointer?
-
Create two
struct student *
's using the create_student()
function from the malloc exercise. These students will have the following
information:
struct student *harry
points at a struct student
with:
- Name: Harry
- zID: 5000000
- Mark: 71.3
struct student *lucy
points at a struct student
with:
- Name: Lucy
- zID: 5111111
- Mark: 62.9
-
Now that we have two struct student
's, we can create our first linked list!
How would we now link these struct
's such that harry
contains a pointer
to lucy
? What value should the next
pointer in the
lucy
struct have?
Afterwards, the linked list should look like this (visually):
-
How would we print out the zID of
lucy
by only using the harry
variable?
-
Having a linked list with 2 nodes is great, but what if we wanted to do 5? Add
3 more students to the linked list after the lucy
node.
Afterwards, the linked list should look similar to this (visually):
-
If we wanted to print the name of all these students, how could that be done
through only the harry
variable?
-
What about a function to print the name of the last student?
How would we define a function that always prints the tail node (last/final node)
of a linked list?
CS Airline
CS Airline is your third and final assignment this term! The theme of the assignment
is managing an airline. Have fun and if you have any questions about the
assignment, feel free to ask in your lab, on the forum or on a
help session!
-
The assignment specification is found on the top bar - just click the button
that says "Assignment 2"!
Have you read the specification?
-
Have you marked Sasha's lecture in your calendar or watched Sasha's lecture
detailing the assignment?
-
Have you downloaded the starter code and have it ready to work on in your
coding environment?