Beispiel #1
0
void displayStrings(List* l)
{
	list_start(l);
	while (list_hasNext(l))
		printf("%s", (char*)list_next(l));
	printf("\n");
}
Beispiel #2
0
void displayIntegers(List* l)
{
	list_start(l);
	while (list_hasNext(l))
		printf("%d ", *((int*)list_next(l)));
	printf("\n");
}
Bool hashtable_contains(HashTable ht, Type key) { //compreuba que exista el key
    if (ht == NULL || key == NULL)
        return FALSE;
    int i;

    i = ht->hashcodeF(key) % ht->capacity;
    if (ht->l_array[i] == NULL)
        return FALSE;
    Iterator ite;
    ite = list_begin(ht->l_array[i]);
    while (list_hasNext(ite)) {
        ite = list_next(ite);
        if (ht->isKeyEqual(retrieveKeyFromIterator(ite), key)) {
            return TRUE;
        }
    }
    return FALSE;
}
Type hashtable_get(HashTable ht, Type key) {  //recupera el value con el key
    if (ht == NULL || key == NULL)
        return NULL;
    int i;

    i = ht->hashcodeF(key) % ht->capacity;
    if (ht->l_array[i] == NULL)
        return NULL;
    Iterator ite;
    ite = list_begin(ht->l_array[i]);
    while (list_hasNext(ite)) {
        ite = list_next(ite);
        if (ht->isKeyEqual(retrieveKeyFromIterator(ite), key)) {
            return retrieveValueFromIterator(ite);
        }
    }
    return NULL;
}
Type* hashtable_getValues(HashTable ht) { //devuelve un arreglo con los key de la tabla
    Type* array; //contiene los values a devolver
    array = (Type *) malloc(sizeof (Type) * ht->size);
    Iterator ite;
    int i;
    int count;
    count = 0;
    for (i = 0; i < ht->capacity; i++) {
        if (ht->l_array[i] != NULL) {
            ite = list_begin(ht->l_array[i]);
            while (list_hasNext(ite)) {
                ite = list_next(ite);
                array[count] = retrieveValueFromIterator(ite);
                count++;
            }
        }
    }
    return array; 
}
void hashtable_put(HashTable ht, Type key, Type value) { 
    int hashcode;
    if (key == NULL || ht == NULL) //clave o ht vacia
        return;
    hashcode = ht->hashcodeF(key);
    if (hashcode > ht->capacity) {
        hashcode = hashcode % ht->capacity; //aritmética modular si clave > capacidad
    }
    int i = hashcode;
    int iguales;
    int flag = 0;
    HtEntry nueva;

    Iterator it;
    if (ht->l_array[i] == NULL) { // si aun no existe la lista la crea
        ht->l_array[i] = list_create();
        nueva = (HtEntry) malloc(sizeof (struct strHtEntry));
        nueva->Value = value;
        nueva->Key = key;
        list_add(ht->l_array[i], nueva); // y agrega la nueva entrada
        ht->size++;
    } else { //si ya existia la lista
        it = list_begin(ht->l_array[i]);
        while (list_hasNext(it)) { //la recorre para ver si ya existe la key
            it = list_next(it);
            iguales = ht->isKeyEqual(retrieveKeyFromIterator(it), key);
            if (iguales == TRUE) { //si ya existía remplaza el valor
                setEntryValue(it, value);
                flag = 1;
            }
        }
        if (flag = 0) { //si recorrió y no encontro la clave debemos agregarla
            nueva = (HtEntry) malloc(sizeof (struct strHtEntry));
            nueva->Value = value;
            nueva->Key = key;
            list_add(ht->l_array[i], nueva);
        }
    }
}
Beispiel #7
0
void exampleList()
{
	// Use pooling for efficiency, if you don't want to use pooling
	// then comment out this line.
	pool_list(16);

	List* L = newList();

	list_add(L, "a");
	list_add(L, "b");
	list_add(L, "c");
	list_add(L, "d");
	list_add(L, "e");
	list_add(L, "f");
	
	// Display the current list
	displayStrings(L);		//ABCDEF

	// Remove first item
	list_removeFirst(L);
	displayStrings(L);		//BCDEF
	
	// Add first item back
	list_addFirst(L, "a");
	displayStrings(L);

	list_clear(L);
	if (list_isEmpty(L))
		printf("List was cleared.\n");

	// Add some strings and remove all that begin with -
	int nums[] = {1, 2, 3, 4, 6, 7, 8};
	int x;
	for (x = 0; x < 7; x++)
		list_add(L, &nums[x]);

	displayIntegers(L);

	list_start(L);
	while (list_hasNext(L))
	{
		// get does not move the current node
		x = *((int*)list_peek(L));
		if (x % 2 == 0)
			// remove will remove the node from the list altogether and
			// return the data removed.
			list_remove(L);
		else
			// next will just goto the next node and return the data.
			list_next(L);
	}
	
	// Print out the odd numbers
	displayIntegers(L);

	// Try removing all while traversing
	list_start(L);
	while (list_hasNext(L))
		list_remove(L);
	
	displayIntegers(L);

	if (L->first == NULL && L->last == NULL && L->size == 0)
		printf("All cleaned up!\n");

	// Traverse through an array of strings and find any that start
	// with . and after it add 0 and add one before it that is -
	list_add(L, ".1");
	list_add(L, " two ");
	list_add(L, ".3");
	list_add(L, " four ");
	list_add(L, ".5");

	displayStrings(L);
	
	list_start(L);
	char* c;
	while (list_hasNext(L))
	{	
		c = (char*)list_peek(L);

		if (c[0] == '.')
		{
			list_insertBefore(L, "-");
			list_insertAfter(L, "0");
		}

		list_next(L);
	}
	displayStrings(L);

	char* first = (char*)L->first->data;
	char* last = (char*)L->last->data;
	if (first[0] == '-' && last[0] == '+')
		printf("Insertions correct.\n");

	// This will clear the list of any nodes and pool them and then free
	// the list itself from memory
	list_free(L);
	
	// If you're not using pooling this can be commented out. This will
	// free all pooled nodes from memory. Always call this at the end 
	// of using any List.
	unpool_list();
}
/**
 * 测试最小生成树
 */
void test_mini_span_tree_from_graph() {
    Student s[] = {
            *studn_get_init(1, "a", 0, 22, 11),
            *studn_get_init(2, "b", 0, 22, 11),
            *studn_get_init(3, "c", 0, 22, 11),
            *studn_get_init(4, "d", 0, 22, 11),
            *studn_get_init(5, "e", 0, 22, 11),
            *studn_get_init(6, "f", 0, 22, 11),
    };

    MstVertex m[] = {
            /*0*/*mst_vertex_get_init((void *)(&s[0]), 0, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[1]), 0, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[2]), 0, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[3]), 0, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[4]), 0, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[5]), 0, (int (*)(const void *, const void *))studn_match, NULL),
            
            /*a点延伸出去的边的连接节点*/
            /*6*/*mst_vertex_get_init((void *)(&s[1]), 7, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[2]), 4, (int (*)(const void *, const void *))studn_match, NULL),
            
            /*b点延伸出去的边的连接节点*/
            /*8*/*mst_vertex_get_init((void *)(&s[0]), 7, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[2]), 6, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[3]), 2, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[5]), 4, (int (*)(const void *, const void *))studn_match, NULL),
            
            /*c点延伸出去的边的连接节点*/
            /*12*/*mst_vertex_get_init((void *)(&s[0]), 4, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[1]), 6, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[4]), 9, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[5]), 8, (int (*)(const void *, const void *))studn_match, NULL),
            
            /*d点延伸出去的边的连接节点*/
            /*16*/*mst_vertex_get_init((void *)(&s[1]), 2, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[5]), 7, (int (*)(const void *, const void *))studn_match, NULL),
            
            
            /*e点延伸出去的边的连接节点*/
            /*18*/*mst_vertex_get_init((void *)(&s[2]), 9, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[5]), 1, (int (*)(const void *, const void *))studn_match, NULL),
            
            /*f点延伸出去的边的连接节点*/
            /*20*/*mst_vertex_get_init((void *)(&s[1]), 4, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[2]), 8, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[3]), 7, (int (*)(const void *, const void *))studn_match, NULL),
            *mst_vertex_get_init((void *)(&s[4]), 1, (int (*)(const void *, const void *))studn_match, NULL),
    };

    Graph graph;
    graph_init(&graph, (int (*)(const void *, const void *))mst_vertex_match, NULL);
    int i;
    //插入所有的节点
    for (i = 0; i < 6; ++i) {
        graph_ins_vertex(&graph, &m[i]);
    }
    printf("%s\n", "insert vertex success");

    //插入所有的边
    graph_ins_edge(&graph, &m[0], &m[6]);
    graph_ins_edge(&graph, &m[0], &m[7]);

    graph_ins_edge(&graph, &m[1], &m[8]);
    graph_ins_edge(&graph, &m[1], &m[9]);
    graph_ins_edge(&graph, &m[1], &m[10]);
    graph_ins_edge(&graph, &m[1], &m[11]);

    graph_ins_edge(&graph, &m[2], &m[12]);
    graph_ins_edge(&graph, &m[2], &m[13]);
    graph_ins_edge(&graph, &m[2], &m[14]);
    graph_ins_edge(&graph, &m[2], &m[15]);

    graph_ins_edge(&graph, &m[3], &m[16]);
    graph_ins_edge(&graph, &m[3], &m[17]);

    graph_ins_edge(&graph, &m[4], &m[18]);
    graph_ins_edge(&graph, &m[4], &m[19]);

    graph_ins_edge(&graph, &m[5], &m[20]);
    graph_ins_edge(&graph, &m[5], &m[21]);
    graph_ins_edge(&graph, &m[5], &m[22]);
    graph_ins_edge(&graph, &m[5], &m[23]);

    List span;
    mst(&graph, m, &span, (int (*)(const void *, const void *))mst_vertex_match);
    list_resetIterator(&span);
    while (list_hasNext(&span)) {
        list_moveToNext(&span);
        MstVertex *vertex = NULL;
        list_iterator(&span, (void **)(&vertex));
        Student *s = (Student *)vertex->data;
        printf("key : %.0f", vertex->key);
        if (vertex->parent != NULL) {
            Student *sP = (Student *)(vertex->parent->data);
            printf(", parentid:%d, ",  sP->_id);
        }
        studn_print(s);
    }

    return;
}