2
votes

I am trying to make a queue list and I am passing reference of link list and its not working and giving error.

In function 'void insertDataToQueue(Node**, int)':| request for member 'next' in '* temp', which is of pointer type 'Node* {aka node*}' (maybe you meant to use '->' ?)|

 void insertDataToQueue(Node **queueList, int burstTime){
    Node *newNode = new Node;
    newNode->burstTime = burstTime;
    newNode->next = NULL;

if(queueList == NULL){
    *queueList = newNode;
}
else{
    Node **temp = queueList;
    while(*temp != NULL)
        temp = *temp->next;
}
}
2
member access has higher precendence than dereference, see here463035818_is_not_a_number
so how can i traverse this list ?Ankit Baid
For what purpose are you writing a datastructure (that already exists in namespace std)?Caleth
I would advise not using pointer-to-pointer parameters (I don't think I've ever had a valid case for using one), instead learn about passing by reference, it makes the syntax much simpler and clearerUnholySheep
Has your question been resolved, by any answer ? If yes, consider to accept answer which resolves your issue. If no, feel free to ask more precision or any thing elseGarf365

2 Answers

2
votes

To iterate over whole list, just a simple pointer to Node is enough :

void insertDataToQueue(Node **queueList, int burstTime){
    Node *newNode = new Node;
    newNode->burstTime = burstTime;
    newNode->next = NULL;

    if(queueList == NULL) {
        *queueList = newNode;
    }
    else {
        Node *temp = *queueList;
        // Find last element, ie element who "next" field is NULL
        while(temp->next != NULL) {
            temp = temp->next;
        }
        // Make last element to point to new element
        temp->next = newNode;
    }
}

Now, without any link, I suppose (and I hope) is only for learning aspect. Because C++ as every container you need. For exemple, you have std::list or std::queue who are linked list. For production code, prefer using it instead of developping by yourself.

0
votes

This

*temp->next;

is parsed as

*(temp->next); 

because -> has higher precedence than *, if you want to first dereference and then access the member you can use brackets:

(*temp)->next;