Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!
Do you have any questions? Have you learned anything that would be useful to share with the rest of the tutorial?
The header contains just enough information to be able to use the typedef struct pointer and functions, but nothing more. So no unsafe access to data, but easy to read access to the functions.
This means it makes sure that everything that's declared in its header file has a complete set of working code so that when the function is called, it will be able to run.
The C file will #include the header so that it can see what declarations it needs to implement.
I then have a main.c that would like to use the functionality offered by the other two files.
Which files need to #include the other files?
The *.c includes the *.h.
Remember that using #include is functionally very similar to copying and pasting the text from the *.h file into the exact position in the file that's including it.
In this way, it's very similar to earlier in the course where we were putting our function declarations at the top of our code files.
Remember that we're compiling all the c files so that all the implementation is translated into instructions for our computer to be able to run. The h files don't need to be compiled because they're included by the c files already.
It is "First In First Out". The oldest element in the queue will be the first to be removed from the queue.
Important elements of this diagram:
The order of the queue is a matter of choice. The front can be the head or the tail, it will just change the way the functions are written.
If we want, we can also remove one of the two and rename it to a top pointer and then only trivial changes would need to be made to the functions for add and remove.
You will need to show what code goes in the header file and what code goes in the implementation file.
We can assume that the data we're holding in each element of the queue is an integer.
typedef struct queueInternals *Queue; void queueAdd(Queue q, int item);
C file:
struct queueInternals { struct queueNode *front; struct queueNode *back; }; struct queueNode { int data; struct queueNode *next; }; // Add a queueNode to the end of the list (just after back) void queueAdd(Queue q, int item) { struct queueNode *newNode = malloc(sizeof (struct queueNode)); if (newNode == NULL) { printf("Could not create queue node, memory allocation failed.\n"); exit(1); } newNode->data = item; newNode->next = NULL; // Add newNode to queue if (q->back == NULL) { // q is empty q->head = newNode; q->back = newNode; } else { // q has at least one node q->back->next = newNode; q->back = newNode; } q->size++; }
Your tutor may still choose to cover some of the questions time permitting.
There are no revision questions as part of this tutorial. Instead, this week, we will release a revision section on the course website that will be a set of revision questions and exercises that cover the majority of the course content.