static void clist_swap_nodes(clist_t* list, clist_node_t* node_a, clist_node_t* node_b) { clist_node_t* pos_a; clist_node_t* pos_b; /* neighbors */ if (node_a->next == node_b) { clist_unlink_node(list, node_a); clist_insert_after(list, node_b, node_a); return ; } else if (node_b->next == node_a) { clist_unlink_node(list, node_b); clist_insert_after(list, node_a, node_b); return ; } /* get posistions */ pos_a = clist_node_next(node_b); pos_b = clist_node_next(node_a); /* unlink nodes */ clist_unlink_node(list, node_a); clist_unlink_node(list, node_b); /* insert nodes */ clist_insert_before(list, pos_a, node_a); clist_insert_before(list, pos_b, node_b); }
int main(void) { link cursor = NULL; int i = 0; clist_print(cursor, print_int_data); for (i = 0; i < 100; i++) { int *p = make_data(i+1); link item = make_node(p); cursor = clist_insert_after(cursor, item); } cursor = cursor->next; clist_print(cursor, print_int_data); printf("ring list length = %d\n", clist_length(cursor)); int step = 0; while (cursor != NULL) { print_int_data(cursor->data); step++; if (step == 3) { printf("-> %d out\n", *(int *)(cursor->data)); cursor = clist_delete(cursor, cursor); printf("length = %d\n", clist_length(cursor)); step = 0; } else cursor = cursor->next; //getchar(); //sleep(1); } return 0; }
int clist_append(clist * lst, void * data) { return clist_insert_after(lst, lst->last, data); }