为什么这个冒泡排序与C中的双链表没有排序?

时间:2014-02-13 05:20:29

标签: c sorting linked-list bubble-sort

我正在尝试使用C中的冒泡排序对双向链表进行排序。但是,返回的列表与我输入的列表相同。示例[1,2,4,8,0,3] - > [1,2,4,8,0,3]

void bublesort(slist_t *list){
    list_item_t *curr = NULL;
    curr = list->head;
    if(curr == NULL){
       printf("\nList is empty.");
    }
    while(curr->next != NULL && curr->value > curr->next->value){
       curr = swap(curr, curr->next, list);
    }
    while(curr->prev != NULL){
        if(curr->value < curr->prev->value){
            bublesort(list);
        }
    }

}

list_item_t *swap(list_item_t *item1, list_item_t *item2, slist_t *list){
    item1->next = item2->next;
    item2->next = item1;
    item2->prev = item1->prev;
    item1->prev = item2;
    if(item1 == list->head){
        list->head = item2;
    }
    if(item2 == list->tail){
        list->tail = item1;
    }
    return item2;
}

0 个答案:

没有答案