} T_END_TEST T_TEST(t_pqueue_alt_insert) { struct pqueue* q = pqueue_create(comp); int i; int num = 10; T_ASSERT(q); for (i = 0; i < num; i++) { T_ASSERT(0 == pqueue_insert(q, (void*)((i % 2) ? i : num - i - (num % 2 ? 1 : 2)))); } T_ASSERT(!pqueue_is_empty(q)); i = 0; while (!pqueue_is_empty(q)) { T_ASSERT(i == (int)pqueue_peek(q)); T_ASSERT(i == (int)pqueue_pop(q)); i++; } T_ASSERT(i == num); pqueue_destroy(q); } T_END_TEST
int main() { unsigned int maxSize = 10; pqueue_t pq = pqueue_empty(maxSize); bool exit = false; char *option = NULL; unsigned int *u = calloc(1,sizeof(unsigned int)); unsigned int v; do { option = print_menu(); switch(*option) { case ADD: printf("\nPor favor ingrese el nodo: "); if(!pqueue_is_full(pq)) { scanf("%u",&v); *u = v; pqueue_enqueue(pq, *u); printf("\nExito.\n"); } else { printf("La cola esta llena\n"); } break; case SHOW: if(!pqueue_is_empty(pq)) { *u = pqueue_fst(pq); printf("\nEl maximo es: %u\n",*u); } else { printf("La cola está vacia\n"); } break; case POP: if(!pqueue_is_empty(pq)) { pqueue_dequeue(pq); printf("\nSe elimino correctamente\n"); } break; case EXIT: exit = true; break; } free(option); option = NULL; } while(!exit); pq = pqueue_destroy(pq); }
void test_new_pqueue_is_empty(void) { setup(); assert_true(pqueue_is_empty(pqueue)); teardown(); }
void test_pqueue_is_not_empty_after_insert(void) { setup(); pqueue_insert(pqueue, (void *) 0UL); assert_false(pqueue_is_empty(pqueue)); teardown(); }
graph_t kruskal(graph_t graph) { graph_t result = graph_empty(graph_vertices_count(graph)); unsigned int L, R, num_edges = graph_edges_count(graph); vertex_t l = NULL, r = NULL; pqueue_t Q = pqueue_empty(num_edges); union_find_t C = union_find_create(graph_vertices_count(graph)); edge_t E = NULL, *edges = graph_edges(graph); for (unsigned int i = 0; i < num_edges; i++) { pqueue_enqueue(Q, edges[i]); } free(edges); edges = NULL; while (!pqueue_is_empty(Q) && union_find_count(C) > 1) { E = edge_copy(pqueue_fst(Q)); l = edge_left_vertex(E); r = edge_right_vertex(E); L = union_find_find(C, vertex_label(l)); R = union_find_find(C, vertex_label(r)); if (L != R) { union_find_union(C, L, R); E = edge_set_primary(E, true); } else { E = edge_set_primary(E, false); } result = graph_add_edge(result, E); pqueue_dequeue(Q); } while (!pqueue_is_empty(Q)) { E = edge_copy(pqueue_fst(Q)); pqueue_dequeue(Q); E = edge_set_primary(E, false); result = graph_add_edge(result, E); } Q = pqueue_destroy(Q); C = union_find_destroy(C); return result; }
void test_pqueue_is_empty_after_last_element_is_removed(void) { setup(); pqueue_insert(pqueue, (void *) 0UL); pqueue_insert(pqueue, (void *) 1UL); pqueue_remove_top(pqueue); pqueue_remove_top(pqueue); assert_true(pqueue_is_empty(pqueue)); teardown(); }
} T_END_TEST T_TEST(t_pqueue_duplicate) { struct pqueue* q = pqueue_create(comp); void* v = (void*)0xdeadbeef; T_ASSERT(q); T_ASSERT(0 == pqueue_insert(q, v)); T_ASSERT(0 == pqueue_insert(q, v)); T_ASSERT(!pqueue_is_empty(q)); T_ASSERT(v == pqueue_peek(q)); T_ASSERT(v == pqueue_pop(q)); T_ASSERT(!pqueue_is_empty(q)); T_ASSERT(v == pqueue_peek(q)); T_ASSERT(v == pqueue_pop(q)); T_ASSERT(pqueue_is_empty(q)); pqueue_destroy(q); } T_END_TEST
Node * pqueue_pop_minimum (PQueue *pqueue) { if (pqueue_is_empty (pqueue)) return NULL; Node *minimum = pqueue->elements[1].data; guint index; swap (pqueue, 1, pqueue->size); pqueue->size--; index = minimum->j * pqueue->width + minimum->i; pqueue->map[index] = -1; sink (pqueue, 1); return minimum; }
graph_t kruskal(graph_t graph) { /* Computes a MST of the given graph. * * This function returns a copy of the input graph in which * only the edges of the MST are marked as primary. The * remaining edges are marked as secondary. * * The input graph does not change. * */ graph_t mst; union_find_t uf; pqueue_t pq; edge_t *edges; edge_t e; unsigned int left, right, n, m; /* Inicialización */ n = graph_vertices_count(graph); m = graph_edges_count(graph); mst = graph_empty(n); uf = union_find_create(n); pq = pqueue_empty(m); /* Llenar `pq` */ edges = graph_edges(graph); for (unsigned int j = 0; j < m; j++) { pqueue_enqueue(pq, edges[j]); } /* Ahora las aristas están en `pq` */ free(edges); edges = NULL; /* Principal */ while (!pqueue_is_empty(pq) && union_find_count(uf) > 1) { e = edge_copy(pqueue_fst(pq)); left = vertex_label(edge_left_vertex(e)); right = vertex_label(edge_right_vertex(e)); left = union_find_find(uf, left); right = union_find_find(uf, right); if (!union_find_connected(uf, left, right)) { e = edge_set_primary(e, true); union_find_union(uf, left, right); } else { e = edge_set_primary(e, false); } mst = graph_add_edge(mst, e); pqueue_dequeue(pq); } /* Agregar aristas restantes como secundarias */ while (!pqueue_is_empty(pq)) { e = edge_copy(pqueue_fst(pq)); e = edge_set_primary(e, false); mst = graph_add_edge(mst, e); pqueue_dequeue(pq); } /* Destroy */ uf = union_find_destroy(uf); pq = pqueue_destroy(pq); return (mst); }