Exemplo n.º 1
0
void exampleArrayList()
{
	ArrayList* l = newArrayList(4);

	alist_add(l, "Aye");
	alist_add(l, "Bee");
	alist_add(l, "Sea");
	alist_add(l, "Dee");
	alist_add(l, "Eee");
	alist_add(l, "Eff");
	alist_add(l, "Gee");
	alist_add(l, "Ach");
	alist_add(l, "Eye");
	alist_add(l, "Jay");
	alist_add(l, "Kay");
	alist_add(l, "Ell");

	printf("Size: %d\n", l->size);

	alist_traverseForward(l, &process);
	printf("\n");

	printf("Remove 'Sea'\n");
	alist_remove(l, "Sea", equals);
	alist_traverseForward(l, &process);
	printf("\n");

	printf("Remove at 0\n");
	alist_removeAt(l, 0);
	alist_traverseForward(l, &process);
	printf("\n");

	printf("Remove at size\n");
	alist_removeAt(l, l->size - 1);
	alist_traverseForward(l, &process);
	printf("\n");

	printf("Insert 'Aye' at 0\n");
	alist_insert(l, "Aye", 0);
	alist_traverseForward(l, &process);
	printf("\n");

	printf("Insert 'Ell' at size\n");
	alist_insert(l, "Ell", l->size);
	alist_traverseForward(l, &process);
	printf("\n");

	printf("Insert 'Sea' at 3\n");
	alist_insert(l, "Sea", 2);
	alist_traverseForward(l, &process);
	printf("\n");

	printf("0: '%s'\n", (char*)alist_get(l, 0));
	printf("3: '%s'\n", (char*)alist_get(l, 3));

	alist_free(l);
}
Exemplo n.º 2
0
static void search(gamestate_t *gs, alist_t *moveStack) {
	if (gamestate_pegs_remaining(gs) == 1) {
//		printf("Found a winning sequence. Final state:\n");
//		gamestate_print(gs);
		
		alist_add(solutions, alist_new_copy(moveStack));
		
		gamesPlayed++;
		
		return;
	}
	
	alist_t *legalMoves = gamestate_legal_moves(gs);
	
	if (alist_is_empty(legalMoves)) {
		gamesPlayed++;
		return;
	}
	
	for (int i = 0; i < legalMoves->size; i++) {
		move_t *m = alist_get(legalMoves, i);
		gamestate_t *nextState = gamestate_apply_move(gs, m);
		alist_add(moveStack, m);
		search(nextState, moveStack);
		alist_remove_last(moveStack);
	}
}
Exemplo n.º 3
0
int main() {
	AssocList *alist = alist_create((CmpFunc)strcmp);

	alist_put(alist, strdup("John"), strdup("+1-234-567-89-10"));
	alist_put(alist, strdup("Alex"), strdup("911"));
	alist_put(alist, strdup("Mike"), strdup("112"));

	for (;;) {
		char *name = read_string("Enter name, 'q' to exit");
		if (strcmp(name, "q") == 0) {
			break;
		} else {
			char *number = alist_get(alist, name);
            char *update_answer;
			if (number == NULL) {
				printf("%s not found.\n", name);
				update_answer = read_string("Add? [y/n]");
			} else {
				printf("%s: %s\n", name, number);
				update_answer = read_string("Update? [y/n]");
			}
            if (update_answer[0] == 'y' || update_answer[0] == 'Y') {
                number = read_string("Enter phone number");
                alist_put(alist, strdup(name), number);
                printf("%s: %s\n", name, number);
            }
            free(update_answer);
			free(name);
		}
	}

	printf("\nYour phonebook:\n");
	alist_foreach(alist, print_phonebook_entry);

	alist_foreach(alist, free_key_and_value);

	alist_destroy(alist);

	return 0;
}
Exemplo n.º 4
0
void* stack_get(stack_t* stack, int index) {
  return alist_get(stack, index);
}