1

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;
}

2 Answers 2

1

The first time you call enQ() it's with a null pointer indicating that a new queue should be created. enQ() creates a new node and returns a pointer to it.

On subsequent calls you are correct that the return value isn't needed as it simply returns back the same Q that was passed in, but the first time the return value is indeed necessary. Without it calling enQ(NULL, i) won't have any way to return the new queue to the caller.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. I must have some fundamental misunderstanding about how pointers are changed in functions. In the Q = new line, is the value of Q not set to new automatically, and hence a return is unnecessary?
So basically, you are doing 2 different operations in the method: queue creation and updation. While updating the queue of course you are updating the content of what Q* is pointing to, so it is okay. But when you created a new queue your local pointer is only pointing to this new queue. How will the calling function know about it, unless you return the pointer or you make the input as Q**.
Ah, so the pointer to allocated memory new is a 'local' variable, in the same way that an int would be, and therefore needs to be returned to the function?
That is correct. If you have an assignment and dereference like *Q = ... then the caller might see the change. But a bare Q = ... only changes the local variable.
0

You don't use pass-through Node to enter enQ, you should create a Queue struct and pass in Queue*

typedef struct Queue {
    struct node* head;
} Queue;

// Adds item to queue
void enQ(struct Queue* Q, int n) {
    struct node* tail;

    struct node* new = malloc(sizeof(struct node));
    new->data = n;
    new->next = NULL;

    if (Q->head == NULL) {
        Q->head = new;
    } else {
        tail = Q->head;
        while (tail->next) {
            tail = tail->next;
        }

        tail->next = new;
    }
}

void deQ(struct Queue* Q, int n) {
    if (Q->head == NULL) {
        return;
    }

    struct node* temp = Q->head;
    Q->head = Q->head->next;
    free(temp);
}

2 Comments

What happens when Q is NULL in if (Q->head == NULL)? Don't you need to check if(Q == NULL)?
@Sisir No need, if you do this, there are too many things to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.