listnode* partition(listnode* A, int B) {
    int d = listCount(A);
    if(d==1){
        return A;
    }
    int i=0;
    listnode *end = A, *temp= A;
    while(end->next != NULL){
        end = end->next;
    }
    listnode *prev = A;
    while(i<d && A!=NULL){
        if(temp->val >= B){
            /*if(temp==end){
                break;
            }*/
           
            if(prev == temp && temp->val >= B){
               
                    listnode *create = listnode_new(temp->val);
                    end->next = create;
                    end = create;
                    prev = temp->next;
                    free(temp);
                    A = temp = prev;    
               
                
            }else{
                listnode *create = listnode_new(temp->val);
                end->next = create;
                end = create;
                prev->next = temp->next;
                free(temp);
                temp = prev->next;
            }
        }else{
            if(temp!=prev){
                prev = prev->next;
            }
            temp = temp->next;
        }
        i++;
    }
    return A;
}
Esempio n. 2
0
// 将数据写入缓存链表尾部
void cache_write(struct Cache *cache, char *buffer, int size) {
    struct ListNode *p = listnode_new(size);
    buffercpy(p->buffer, buffer, size);

    // 加读写锁
    pthread_rwlock_wrlock(&cache->lock);
    if(cache->writeIndex == NULL) {
        cache->writeIndex = p;
    } else {
        cache->writeIndex->next = p;
        cache->writeIndex = cache->writeIndex->next;
    }

    if(cache->readIndex == NULL) cache->readIndex = cache->writeIndex;

    cache->chuncks++;
    cache->chuncks = cache->chuncks % 10000;

    pthread_rwlock_unlock(&cache->lock);
}
Esempio n. 3
0
int main(int argc, char const *argv[])
{
    listnode *n1 = listnode_new(1);
    listnode *n2 = listnode_new(2);
    listnode *n3 = listnode_new(3);
    listnode *n4 = listnode_new(40);
    listnode *n5 = listnode_new(50);
    listnode *n6 = listnode_new(60);
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = n6;
    n6->next = NULL;

    listnode *x = subtract(n1);
    while(x!=NULL) {
        printf("X %d\n", x->val);
        x = x->next;
    }
    return 0;
}