Esempio n. 1
0
 int main(void)
 {
 	int i;
 	int a[10];
 	int type;
 	int n;
 	
 	printf("enter the array size\n");
 	scanf("%i", &n);
 	
 	printf("enter your array values\n");
 	for(i = 0; i < n;i++)
 	{
 		scanf("%i", &a[i]);
 	}
 	
 	printf("array before sort\n");
 	
 	for(i = 0;i < n; i++)
 	{
 		printf("%i ", a[i]);
 	}
 	printf("\n");
 	
 	printf("enter the type of sort ascend(enter 0) descend(1)");
 	scanf("%i", &type);
 	
 	ascend(a, n, type);
 	for(i = 0; i < n; i++)
 	{
 		printf("%i\n", a[i]);
 	}
 	
 	return 0;
 }
Esempio n. 2
0
/*
 * ascends iterator a until it matches iterator b's depth.
 *
 * Returns -1 if they end up on the same k (meaning a < b).
 * Returns 0 otherwise.
 */
static int elevate(btree_iterator a, btree_iterator b)
{
	while (a->node->depth < b->node->depth)
		ascend(a);

	if (a->k == b->k)
		return -1;
	return 0;
}
Esempio n. 3
0
void RoboMiner::action()
{

//	if(rand() % 512 < 48)
//		scan();

//	std::cout << "E: " << energy << std::endl;

	if(ascending){
		ascend();
		return;
	}

	if(drillCell){
		--energy;
		drill(drillCell->getY(), drillCell->getX());
		return;
	}

	if(cell->mineralCount() > 0 && !isFull()){
		--energy;
		mine();
		return;
	}

	if(!destCell || exploring){
		energy -= 2;
		scan();
	}

	if(destCell){
		navigate();
	}else{
		int offset_x=0, offset_y=0;
		int roll = rand() % 512;

		if(roll >= 255){
			offset_y = (roll % 2) ? 1 : -1;
		}else{
			offset_x = (roll % 2) ? 1 : -1;
		}

/*		std::cout << "[action] off_x: " << offset_x << "  off_y: "
			  << offset_y << "  from roll: " << roll
			  << std::endl;
*/
		move(cell_y + offset_y, cell_x + offset_x);
	}

	
}
Esempio n. 4
0
int btree_cmp_iters(const btree_iterator iter_a, const btree_iterator iter_b)
{
	btree_iterator a = {*iter_a}, b = {*iter_b};
	int ad, bd;

	ad = btree_deref(a);
	bd = btree_deref(b);

	/* Check cases where one or both iterators are at the end. */
	if (!ad)
		return bd ? 1 : 0;
	if (!bd)
		return ad ? -1 : 0;

	/* Bring iterators to the same depth. */
	if (a->node->depth < b->node->depth) {
		if (elevate(a, b))
			return -1;
	} else if (a->node->depth > b->node->depth) {
		if (elevate(b, a))
			return 1;
	}

	/* Bring iterators to the same node. */
	while (a->node != b->node) {
		ascend(a);
		ascend(b);
	}

	/* Now we can compare by k directly. */
	if (a->k < b->k)
		return -1;
	if (a->k > b->k)
		return 1;

	return 0;
}
Esempio n. 5
0
int btree_deref(btree_iterator iter)
{
	if (iter->k >= iter->node->count) {
		struct btree_iterator_s tmp = *iter;
		do {
			if (!ascend(iter)) {
				*iter = tmp;
				return 0;
			}
		} while (iter->k >= iter->node->count);
	}

	iter->item = (void*)iter->node->item[iter->k];
	return 1;
}
Esempio n. 6
0
int btree_prev(btree_iterator iter)
{
	if (iter->node->depth) {
		branch_end(iter);
	} else if (iter->k == 0) {
		struct btree_iterator_s tmp = *iter;
		do {
			if (!ascend(iter)) {
				*iter = tmp;
				return 0;
			}
		} while (iter->k == 0);
	}

	iter->item = (void*)iter->node->item[--iter->k];
	return 1;
}
Esempio n. 7
0
File: main.c Progetto: S010/misc
void
handle_icon_view_item_activated(GtkWidget *widget, GtkTreePath *tree_path, gpointer unused) {
    const char *opener = "/home/s/bin/op";
    char *argv[] = { NULL, NULL, NULL };
    char *new_path = NULL;
    char *file_path = NULL;
    gint *pidx;
    pid_t pid;
    fsnode_t *child;

    pidx = gtk_tree_path_get_indices(tree_path);
    /* they directly correlate to fsents in current_fsdir */
    if (*pidx < 0 || *pidx >= current_fsnode->nchildren)
        return;
    child = current_fsnode->children + *pidx;

    if (child->type == DIRECTORY) {
        if (!strcmp(child->name, ".."))
            new_path = ascend(current_fsnode->path);
        else
            new_path = str_glue(current_fsnode->path, slash(current_fsnode->path), child->name, NULL);
        change_dir(new_path);
        free(new_path);
    } else {
        pid = fork();

        if (pid > 0)
            return;
        else if (pid < 0) {
            warn("fork");
            return;
        } else {
            file_path = str_glue(current_fsnode->path, slash(current_fsnode->path), child->name, NULL);
            argv[0] = strdup(opener);
            argv[1] = strdup(file_path);
            if (execv("/home/s/bin/op", argv))
                err(1, "execlp");
        }
    }
}
Esempio n. 8
0
static RBNode *
rb_inverted_iterator(RBTree *rb)
{
	RBNode	   *node = rb->cur;

restart:
	switch (node->iteratorState)
	{
		case InitialState:
			if (node->left != RBNIL)
			{
				node->iteratorState = FirstStepDone;
				descend(node->left);
			}
			/* FALL THROUGH */
		case FirstStepDone:
			if (node->right != RBNIL)
			{
				node->iteratorState = SecondStepDone;
				descend(node->right);
			}
			/* FALL THROUGH */
		case SecondStepDone:
			node->iteratorState = ThirdStepDone;
			return node;
		case ThirdStepDone:
			if (node->parent)
				ascend(node->parent);
			break;
		default:
			elog(ERROR, "unrecognized rbtree node state: %d",
				 node->iteratorState);
	}

	return NULL;
}
Esempio n. 9
0
int main()
{
    int choice,data,pos;
    header=NULL;

    while(1)
    {
        system("cls");
        printf("\nSelect your choice: ");
        printf("\n1. Create List ");
        printf("\n2. Insert Node ");
        printf("\n3. Display List ");
        printf("\n4. Delete Node ");
        printf("\n5. Arrange in Ascending order");
        printf("\n6. Exit\n");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1: create();
                    break;

            case 2: system("cls");
                    printf("\nSelect your choice: ");
                    printf("\n1. Insert at the End ");
                    printf("\n2. Insert at the Beginning ");
                    printf("\n3. Insert at a Specified Position ");
                    printf("\n4. Back to main menu\n");
                    scanf("%d",&choice);

                    switch(choice)
                    {
                    case 1: system("cls");
                            printf("\nEnter the data of the node: ");
                            scanf("%d",&data);
                            insert_end(data);
                            printf("\nNode inserted at the end of the list");
                            break;

                    case 2: system("cls");
                            printf("\nEnter the data of the node: ");
                            scanf("%d",&data);
                            insert_beg(data);
                            printf("\nNode inserted at the beginning of the list");
                            break;

                    case 3: system("cls");
                            printf("\nEnter the position at which you want to insert the node: ");
                            scanf("%d",&pos);
                            printf("\nEnter the data of the node: ");
                            scanf("%d",&data);
                            insert_pos(data,pos);
                            break;

                    case 4: break;

                    default: printf("Invalid choice!!");

                    }
                    break;

            case 3: display();
                    break;

            case 4: system("cls");
                    printf("\nSelect your choice: ");
                    printf("\n1. Delete from the End ");
                    printf("\n2. Delete from the Beginning ");
                    printf("\n3. Delete from a Specified Position ");
                    printf("\n4. Delete by the data of the node");
                    printf("\n5. Back to main menu\n");
                    scanf("%d",&choice);

                    switch(choice)
                    {
                    case 1: system("cls");
                            delete_end();
                            printf("\nNode deleted from the end of the list");
                            break;

                    case 2: system("cls");
                            delete_beg();
                            printf("\nNode deleted from the beginning of the list");
                            break;

                    case 3: system("cls");
                            printf("\nEnter the position at which you want to delete the node: ");
                            scanf("%d",&pos);
                            delete_pos(pos);
                            break;

                    case 4: system("cls");
                            printf("\nEnter the data which you want to delete: ");
                            scanf("%d",&data);
                            delete_val(data);
                            break;

                    case 5: break;

                    default: printf("Invalid choice!!");

                    }
                    break;

            case 5: ascend();
                    system("cls");
                    printf("\nList rearranged in ascending order");
                    getch();
                    break;

            case 6: exit(0);
                    break;

            default: printf("Invalid choice!!");
                     getch();

        }
    }
    return 0;
}
Esempio n. 10
0
int btree_remove_at(btree_iterator iter)
{
	struct btree *btree = iter->btree;
	struct btree_node *root;

	if (!btree_deref(iter))
		return 0;

	if (!iter->node->depth) {
		node_remove_leaf_item(iter->node, iter->k);
		if (iter->node->count >= MIN || !iter->node->parent)
			goto finished;
	} else {
		/*
		 * We can't remove an item from an internal node, so we'll replace it
		 * with its successor (which will always be in a leaf), then remove
		 * the original copy of the successor.
		 */

		/* Save pointer to condemned item. */
		const void **x = &iter->node->item[iter->k];

		/* Descend to successor. */
		iter->k++;
		branch_begin(iter);

		/* Replace condemned item with successor. */
		*x = iter->node->item[0];

		/* Remove successor. */
		node_remove_leaf_item(iter->node, 0);
	}

	/*
	 * Restore nodes that fall under their minimum count.  This may
	 * propagate all the way up to the root.
	 */
	for (;;) {
		if (iter->node->count >= MIN)
			goto finished;
		if (!ascend(iter))
			break;
		node_restore(iter->node, iter->k);
	}

	/*
	 * If combining came all the way up to the root, and it has no more
	 * dividers, delete it and make its only branch the root.
	 */
	root = iter->node;
	assert(root == btree->root);
	assert(root->depth > 0);
	if (root->count == 0) {
		btree->root = root->branch[0];
		btree->root->parent = NULL;
		free(root);
	}

finished:
	btree->count--;
	iter->node = NULL;
	return 1;
}
Esempio n. 11
0
void btree_insert_at(btree_iterator iter, const void *item)
{
	const void *x = item;
	struct btree_node *xr = NULL;
	struct btree_node *p;
	struct btree *btree = iter->btree;

	/* btree_insert_at always sets iter->item to item. */
	iter->item = (void*)item;

	/*
	 * If node is not a leaf, fall to the end of the left branch of item[k]
	 * so that it will be a leaf. This does not modify the iterator's logical
	 * position.
	 */
	if (iter->node->depth)
		branch_end(iter);

	/*
	 * First try inserting item into this node.
	 * If it's too big, split it, and repeat by
	 * trying to insert the median and right subtree into parent.
	 */
	if (iter->node->count < MAX) {
		node_insert(x, xr, iter->node, iter->k);
		goto finished;
	} else {
		for (;;) {
			node_split(&x, &xr, iter->node, iter->k);

			if (!ascend(iter))
				break;

			if (iter->node->count < MAX) {
				node_insert(x, xr, iter->node, iter->k);
				goto finished;
			}
		}

		/*
		 * If splitting came all the way up to the root, create a new root whose
		 * left branch is the current root, median is x, and right branch is the
		 * half split off from the root.
		 */
		assert(iter->node == btree->root);
		p = node_alloc(1);
		p->parent = NULL;
		p->count = 1;
		p->depth = btree->root->depth + 1;
		p->item[0] = x;
		p->branch[0] = btree->root;
			btree->root->parent = p;
			btree->root->k = 0;
		p->branch[1] = xr;
			xr->parent = p;
			xr->k = 1;
		btree->root = p;
	}

finished:
	btree->count++;
	iter->node = NULL;
}