int main(void) { struct CELL head; struct CELL *p; int data; int k; head.next = head.prev = p = &head; scanf("%d", &data); while (data > 0) { p->next = (struct CELL*)malloc(1 * sizeof(struct CELL)); head.prev = p->next; p->next->prev = p; p = p->next; p->data = data; scanf("%d", &data); } p->next = &head; print_list(&head); scanf("%d", &k); cell_swap(&head, k); print_list(&head); return 0; }
void bases::sort() { CALLSTACKITEM_N(_CL("bases"), _CL("sort")); auto_ptr< CArrayFixFlat<cell_list_node*> > arr(new CArrayFixFlat<cell_list_node*>(256)); arr->SetReserveL( last_cell->pos ); cell_list_node* n=first_cell; while (n) { arr->AppendL(n); n=n->next; } User::QuickSort(arr->Count(), cell_key(*arr), cell_swap(*arr)); int i; double cum_t=0.0; first_cell=last_cell=0; cell_list_node* prev=0; cell_list_node** put_into=&first_cell; for (i=0; i<arr->Count(); i++) { n=(*arr)[i]; cum_t+=n->t; n->cum_t=cum_t; n->pos=i; n->prev=prev; prev=n; *put_into=n; put_into=&(n->next); } last_cell=n; total_t=cum_t; up_to_date_cum_pos=n->pos; n->next=0; }