chaos_queue.c
void* chaos_queue_rand_dequeue(chaos_queue* queue)
{
    queue_entry* entry;
    void* data;
    int index;
    
    /* Sanity check */
    if(queue == NULL)
        return NULL;
    
    if(queue->count == 0)
        return NULL;
    
    data = NULL;
    
    index = rand() % queue->count;
    
    if(index == (queue->count-1)) {
        /* Tail of the list, just dequeue as normal */
        data = chaos_queue_dequeue(queue);
    }
    else {
        if(index == 0) {
            /* This is the head */
            entry = queue->head;
            queue->head = queue->head->next;
            
            if(queue->head == NULL) {
                queue->tail = NULL;
            }
            else {
                queue->head->prev = NULL;    
            }
        }
        else {
            /* Remove from middle; move forward the number of elements */
            entry = queue->head;
            while(index > 0) {
                entry = entry->next;
                index--;
            }
            
            entry->prev->next = entry->next;
            entry->next->prev = entry->prev;
        }
        
        queue->count--;
        data = entry->data;
        free(entry);
    }
    
    return data;
}
/* Removes data from a random point in the queue */
Removes data from a random point in the queue
Removes data from a random point in the queue
void* chaos_queue_rand_dequeue(chaos_queue* queue)
{
    queue_entry* entry;
    void* data;
    int index;
    if(queue == NULL)
        return NULL;
    if(queue->count == 0)
        return NULL;
    data = NULL;
    index = rand() % queue->count;
    if(index == (queue->count-1)) {
        data = chaos_queue_dequeue(queue);
    }
    else {
        if(index == 0) {
            entry = queue->head;
            queue->head = queue->head->next;
            if(queue->head == NULL) {
                queue->tail = NULL;
            }
            else {
                queue->head->prev = NULL;    
            }
        }
        else {
            entry = queue->head;
            while(index > 0) {
                entry = entry->next;
                index--;
            }
            entry->prev->next = entry->next;
            entry->next->prev = entry->prev;
        }
        queue->count--;
        data = entry->data;
        free(entry);
    }
    return data;
}
void * chaos_queue_rand_dequeue ( chaos_queue * queue ) { queue_entry * entry ; void * data ; int index ; if ( queue == NULL ) return NULL ; if ( queue -> count == 0 ) return NULL ; data = NULL ; index = rand ( ) % queue -> count ; if ( index == ( queue -> count - 1 ) ) { data = chaos_queue_dequeue ( queue ) ; } else { if ( index == 0 ) { entry = queue -> head ; queue -> head = queue -> head -> next ; if ( queue -> head == NULL ) { queue -> tail = NULL ; } else { queue -> head -> prev = NULL ; } } else { entry = queue -> head ; while ( index > 0 ) { entry = entry -> next ; index -- ; } entry -> prev -> next = entry -> next ; entry -> next -> prev = entry -> prev ; } queue -> count -- ; data = entry -> data ; free ( entry ) ; } return data ; }
Removes data from a random point in the queue
Removes data from a random point in the queue
/* Sanity check */ /* Tail of the list, just dequeue as normal */ /* This is the head */ /* Remove from middle; move forward the number of elements */