Exemplo n.º 1
0
Arquivo: avl.c Projeto: hubugui/ds
void avl_bfs_dump(struct avl *avl, dump dmp)
{
    struct avl_node *node = avl->root;
    struct linklist *list;
    int depth = 0, idx = 0, tmp = 0;
    int pre_depth = -1, pre_idx_line = -1;
    int height = avl_height(avl);

    if (!node)
        return;
    if ((list = linklist_new()) == NULL)
        return;

    if (linklist_insert(list, node, (void *) (long int) depth, NULL))
        goto fail;

    while (linklist_size(list) > 0) {
        node = (struct avl_node *) linklist_remove_head(list, 
                                                        (void **) &depth, 
                                                        (void **) &tmp);

        if (node)
            _node_bfs_dump(node->value, depth, idx, 
                            height,  
                            dmp,
                            &pre_depth, &pre_idx_line);
        if (linklist_insert(list, node ? node->left : NULL, 
                            (void *) (long int) (depth + 1), NULL))
            goto fail;
        if (linklist_insert(list, node ? node->right : NULL, 
                            (void *) (long int) (depth + 1), NULL))
            goto fail;
        if (idx + 1 == (int) pow(2, height + 1))
            break;
        idx++;
    }

    printf("\n");
fail:
    linklist_delete(list);
}
Exemplo n.º 2
0
int main()
{
    linklist H1, H2;
    int a[][2] = {{5, 0}, {2, 1}, {9, 4}, {3, 16}, {8, 8}, {-30, 27}};
    int b[][2] = {{23, 16}, {6, 1}, {-8, 8}, {11, 11}, {25, 25}, {27, 27}};
    int i, j;
    H1 = linklist_create();
    H2 = linklist_create();

    for (i = 0; i < sizeof(a)/sizeof(a[0]); i++)
        linklist_insert(H1, a[i][0], a[i][1]);
            
    for (i = 0; i < sizeof(b)/sizeof(b[0]); i++)
        linklist_insert(H2, b[i][0], b[i][1]);

    linklist_show(H1);
    printf("\n");
    linklist_show(H2);

    printf("******\n");
    linklist_union(H1, H2);
    linklist_show(H1);
    return 0;
}
Exemplo n.º 3
0
int main(int argc, const char *argv[])
{
	Linklist *testList = NULL;
	int i;
	srand(time(0));
	printf("list length is %d\n",linklist_length(testList));
	for(i = 0; i < 10 ; i++){
		/* node->value = rand()%100+1; */
		linklist_insert(&testList, i,0);
	}
	printf("list length is %d\n",linklist_length(testList));
	linklist_print(testList);
	linklist_remove(&testList, 9);
	printf("list length is %d\n",linklist_length(testList));
	linklist_print(testList);
	linklist_destroy(&testList);
	linklist_print(testList);
	return 0;
}