I'm having trouble interpreting the code for a linked list of students passed out as preparation for pset5, and would like to know if someone could explain what I am missing.
The structs are as follows ...
typedef struct node
{
student* student;
struct node* next;
}node;
And ...
typedef struct
{
int id;
char* name;
char* house;
}student;
Here's how I am thinking about this, and I know something is wrong. Two lines in particular are troubling me. First, node* ptr = first.
The variable ptr
should hold only the address of node* first
which has been set to null with node* first = NULL.
The node pointer first
has no "compartment" for any other data (since we always draw it as a single box, so node* ptr = first
means ptr
only contains an address, which is NULL. Similarly, node* predptr = ptr
indicates predptr
should hold just the address of ptr.
Here's the big problem: based on predptr->student != NULL
it seems predptr
holds a lot more than an address, even though it's a pointer. It appears to have access to everything that ptr
contains, though no dereferencing occurs. Could someone tell me how best to think about this code in layman's terms?
// free list before quitting
node* ptr = first;
while (ptr != NULL);
{
node* predptr = ptr;
ptr = ptr->next;
if (pedptr->student != NULL)
{ etc