Currently preparing for exams and can't figure out the reasoning behind the queue[rear*] = new_node in this example code.
are you not putting the address of new_node in rather than the value?
Also, is the queue a Node ** queue because it is a pointer to a list of node pointers?
Thanks so much, I really appreciate it, no matter how many hours i spend on double pointers, they always crop up and retest my understanding i thought i finally had!
void enQueue(struct node **queue, int *rear, struct node *new_node)
{
queue[*rear] = new_node;
(*rear)++;
}
struct node *deQueue(struct node **queue, int *front)
{
(*front)++;
return queue[*front - 1];
}
the variable rear
is a pointer to an int
. With *rear
you get the value of that int
. That value is then used as index.
It's equivalent to e.g.
int index = *rear;
queue[index] = new_node;
index++;
*rear = index;
It copies the value of the variable new_node
(i.e. the address of where the pointer is pointing) into queue[index]
. From this point onward, both new_node
and queue[index]
points to the same thing.
I hope that makes it clearer what's happening.