I am having some confusion regarding queues. When I return a pointer to the start of a queue as shown in the code below, and use
Q = enQ(Q)
the function works fine. However, I don't see why the function needs to return anything, since the pointer to queue is being updated within. Why does the following not seem to work when the function is made to return void?
enQ(Q)
Code:
// Adds item to queue
struct node* enQ(struct node* Q, int n){
struct node* last = Q;
struct node* new = malloc(sizeof(struct node));
new->data = n;
new->next = NULL;
if (!Q){
Q = new;
} else {
while (last->next){
last = last->next;
}
last->next = new;
}
return Q;
}