Question: I have been trying hard to resolve this however yet not succeed I have data structs as follow (which actually is very complex I just simplifies for discussion) :

```
typedef struct node{
 struct node* next;
  void* arg;
}node_t;

typedef struct queue{
node_t* head;
node_t* tail;
}queue_t;

addQ(queue_t*ptr , int data)
{
    queue_t* q = ptr;
    node_t * n = malloc(sizeof(*n));

    n->arg = data;
    n->next = NULL;

    if(NULL == q->head){
       q->head = q->tail = n;
       return ;
    }
    q->tail->next = n;
    q->tail = q->tail->next;
}

```

Now I want to delete node of same value ( I have tried couple ways however yet not succeed ) , Just consider this sequence for reference:

```
addQ(q, 12);
addQ(q, 12);
addQ(q,  4);
addQ(q, 12);
addQ(q, 12);
addQ(q, 14);
addQ(q, 12);
addQ(q, 12);

```

I want to Delete all the nodes with value 12.

Answer: 
This solution got a bit hairy with the double pointers, but I still like it, as it doesn't have to special case what node (first vs the rest) is being checked. I tried to put enough comments in to describe what's going on, but it's still hard for even me to follow at first glance.

PSEUDOCODE..

```
Queue * q;
VALUE = 12;

// double pointer so we can treat the queue head and subsequent nodes the same.
//   because they are both pointers to Node.  
// Otherwise you'd have to have code that says if the one you're removing is the 
// first element of the queue, adjust q->head, otherwise adjust node->next.  
// This lets you not special case the deletion.
Node ** node_ptr = &(q->head)

while (*node_ptr != null) {
    if ((**node_ptr).arg == VALUE) {
        // store off the matching node to be freed because otherwise we'd orphan
        // it when we move the thing pointing to it and we'd never be able to free it
        Node * matched_node = *node_ptr;

        // when we find a match, don't move where node_ptr points, just change the value it
        // points to to skip the matched node and point to the one after it (or null)
        *node_ptr = matched_node->next; 
        free(matched_node);
    } else {
        // otherwise, nothing was deleted, so skip over that node to the next one.
        // remember, **node_ptr is a double dereference, so we're at the node
        // now, so then we grab the address of the non-matching node's next value so it can be
        // potentially changed in the next iteration
        node_ptr = &((**node_ptr).next);
    }
}

```
Here's an inline solution that doesn't use double pointers. It has to treat the first element and subsequent elements differently since the pointer to adjust changes from the queue structure to the node structure.

Also, for subsequent nodes, you have to track the trailing node, since that's where you have to make the adjustment as you delete the matching node.

```
Queue * q;
VALUE = 12;

// handle the case where the first node matches.
// you have to adjust the q's head pointer
// delete from the head and set a new head node until a non-matching head is found
while (q->head != NULL && q->head->arg == VALUE) {
  Node * matching_node = q->head;
  q->head = q->head->next;
  free(matching_node);
}

// if there is more than one node left, need to check the subsequent nodes
if (q->head != NULL && q->head->next != NULL) {

    Node * node_ptr = q->head->next;
    Node * prev_node_ptr = q->head;

    while (node_ptr != NULL) {
        if (node_ptr->arg == VALUE) {
            Node * matched_node = node_ptr; // don't orphan it before it's freed

            // You don't move the prev_node pointer since that doesn't change when a match
            //   is found.  Only the node_ptr, which skips to the next one. 
            node_ptr = node_ptr->next;
            free(matched_node);            
        } else {
            prev_node_ptr = node_ptr;
            node_ptr = node_ptr->next;
        }
    }
}

```