Exemple #1
0
int traverseDFS(Graph graph, int start, Dllist close) {
	Dllist node, stack;
	JRB visited;
	int output[100];
	int temp;

	int i, n, counter = 0;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, new_jval_i(start));

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			counter++;
			// reportFunc(temp);
			dll_append(close, new_jval_i(temp));
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_prepend(stack, new_jval_i(output[i]));
				}
			}
		}
	}

	return counter;
}
Exemple #2
0
main()
{
  IS is;
  Queue q;
  Stack s;
  Dllist l;
  int i;
  Jval j;

  is = new_inputstruct(NULL);

  while (get_line(is) > 0) {
    q = new_queue();
    s = new_stack();
    l = new_dllist();
    for (i = 0; i < strlen(is->fields[0]); i++) {
      queue_enqueue(q, new_jval_c(is->fields[0][i]));
      stack_push(s, new_jval_c(is->fields[0][i]));
      dll_append(l, new_jval_c(is->fields[0][i]));
      dll_prepend(l, new_jval_c(is->fields[0][i]));
    }
    while (!queue_empty(q)) {
      j = queue_dequeue(q); printf("%c", j.c);
      j = stack_pop(s); printf("%c", j.c);
      printf("%c", l->flink->val.c); dll_delete_node(l->flink);
      printf("%c", l->flink->val.c); dll_delete_node(l->flink);
      printf(" ");
    }
    printf("\n");
    free_queue(q);
    free_stack(s);
    free_dllist(l);
  }
}
Exemple #3
0
int solution(Graph graph, Jval start, Jval stop, Dllist stackVisit, Jval (*cloneFunc)(Jval),
	int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist stackRes;
	Dllist node;
	Jval nowNode, temp;
	int counter = 0;

	stackRes = new_dllist();
	node = dll_first(stackVisit);
	nowNode = cloneFunc(node->val);
	dll_delete_node(node);
	dll_prepend(stackRes, nowNode);

	while(!dll_empty(stackVisit)) {
		if(isAdjacent(graph, nowNode, start, compare)) {
			dll_prepend(stackRes, start);
			counter++;
			break;
		}

		do {
			node = dll_first(stackVisit);
			temp = cloneFunc(node->val);
			dll_delete_node(node);	

			if(isAdjacent(graph, nowNode, temp, compare)) {
				dll_prepend(stackRes, temp);
				nowNode = temp;
				counter++;
				break;
			}
		} while(!dll_empty(stackVisit));
	}

	printf("Solution: The shortest path between two node: \n");

	while(!dll_empty(stackRes)) {
		node = dll_first(stackRes);
		reportFunc(node->val);
		dll_delete_node(node);
	}

	free_dllist(stackVisit);

	return counter;
}
Exemple #4
0
void DFS(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	Dllist node, stack;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return;
	}

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				free(output);
				return;
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_prepend(stack, output[i]);
				}
			}
		}
	}
}
Exemple #5
0
int deepFirstSearch(Graph graph, int start, int stop, Dllist close) {
	Dllist node, stack;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, new_jval_i(start));

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			// reportFunc(temp);
			dll_append(close, new_jval_i(temp));
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				return 1;
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_prepend(stack, new_jval_i(output[i]));
				}
			}
		}
	}

	jrb_free_tree(visited);
	free_dllist(stack);

	return 0;
}
Exemple #6
0
int main(void) {
	A* a = malloc(sizeof(A)), *b = malloc(sizeof(A)), *c = NULL;
	List* l = list_init();
	Stack* s = stack_init();
	Queue* q = queue_init();
	DoubleLinkedList* d = dll_init();
	printf("a: %d\nB: %d\nc: %d\n", (int)a, (int)b, (int)c);
	a->a1 = 1;
	a->a2 = 'c';
	b->a1 = 2;
	b->a2 = 'a';

	printf("\n=== LIST TEST ===\n");
	list_add(l, a, 0);
	list_append(l, b);
	list_remove(l, 0);
	list_print(l);	
	c = list_get(l, 0);
	printf("c: %d\n", (int)c);
	list_delete(l);

	printf("\n=== STACK TEST ===\n");
	stack_push(s, b);
	stack_push(s, a);
	stack_pop(s);
	stack_print(s);
	c = stack_peek(s);
	printf("c: %d\n", (int)c);
	stack_delete(s);

	printf("\n=== QUEUE TEST ===\n");
	queue_push(q, a);
	queue_push(q, b);
	queue_pop(q);
	queue_print(q);
	c = queue_peek(q);
	printf("c: %d\n", (int)c);
	queue_delete(q);

	printf("\n=== DOUBLE LINKED LIST TEST ===\n");
	dll_add(d, b, 0);
	dll_prepend(d, a);
	dll_remove(d, 1);
	dll_print(d);
	c = dll_get(d, 0);
	printf("c: %d\n", (int)c);
	dll_delete(d);

	free(a);
	free(b);
	return 0;
}
Exemple #7
0
int UShortestPath(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist node, queue, stackVisit;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return 0;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			jrb_insert_gen(visited, temp, temp, compare);
			dll_prepend(stackVisit, temp);
			
			if(compare(temp, stop) == 0) {
				return solution(graph, start, stop, stackVisit, cloneFunc, compare, reportFunc);
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return -1;
}
Exemple #8
0
int UShortestPath(Graph graph, int start, int stop, Dllist close) {
	Dllist node, queue, stackVisit;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			jrb_insert_int(visited, temp, new_jval_i(temp));
			dll_prepend(stackVisit, new_jval_i(temp));
			
			if(temp == stop) {
				return solution(graph, start, stop, stackVisit, close);
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return -1;
}
Exemple #9
0
/* Get flow ring ID, if not present try to create one */
static INLINE int
dhd_flowid_lookup(dhd_pub_t *dhdp, uint8 ifindex,
                  uint8 prio, char *sa, char *da, uint16 *flowid)
{
	uint16 id;
	flow_ring_node_t *flow_ring_node;
	flow_ring_table_t *flow_ring_table;
	unsigned long flags;

	DHD_INFO(("%s\n", __FUNCTION__));

	if (!dhdp->flow_ring_table)
		return BCME_ERROR;

	flow_ring_table = (flow_ring_table_t *)dhdp->flow_ring_table;

	id = dhd_flowid_find(dhdp, ifindex, prio, sa, da);

	if (id == FLOWID_INVALID) {

		if_flow_lkup_t *if_flow_lkup;
		if_flow_lkup = (if_flow_lkup_t *)dhdp->if_flow_lkup;

		if (!if_flow_lkup[ifindex].status)
			return BCME_ERROR;

		id = dhd_flowid_alloc(dhdp, ifindex, prio, sa, da);
		if (id == FLOWID_INVALID) {
			DHD_ERROR(("%s: alloc flowid ifindex %u status %u\n",
			           __FUNCTION__, ifindex, if_flow_lkup[ifindex].status));
			return BCME_ERROR;
		}

		/* register this flowid in dhd_pub */
		dhd_add_flowid(dhdp, ifindex, prio, da, id);
	}

	ASSERT(id < dhdp->num_flow_rings);

	flow_ring_node = (flow_ring_node_t *) &flow_ring_table[id];
	DHD_FLOWRING_LOCK(flow_ring_node->lock, flags);
	if (flow_ring_node->active) {
		DHD_FLOWRING_UNLOCK(flow_ring_node->lock, flags);
		*flowid = id;
		return BCME_OK;
	}
	/* Init Flow info */
	memcpy(flow_ring_node->flow_info.sa, sa, sizeof(flow_ring_node->flow_info.sa));
	memcpy(flow_ring_node->flow_info.da, da, sizeof(flow_ring_node->flow_info.da));
	flow_ring_node->flow_info.tid = prio;
	flow_ring_node->flow_info.ifindex = ifindex;
	flow_ring_node->active = TRUE;
	flow_ring_node->status = FLOW_RING_STATUS_PENDING;
	DHD_FLOWRING_UNLOCK(flow_ring_node->lock, flags);
	DHD_FLOWID_LOCK(dhdp->flowid_lock, flags);
	dll_prepend(&dhdp->bus->const_flowring, &flow_ring_node->list);
	DHD_FLOWID_UNLOCK(dhdp->flowid_lock, flags);

	/* Create and inform device about the new flow */
	if (dhd_bus_flow_ring_create_request(dhdp->bus, (void *)flow_ring_node)
	        != BCME_OK) {
		DHD_ERROR(("%s: create error %d\n", __FUNCTION__, id));
		return BCME_ERROR;
	}

	*flowid = id;
	return BCME_OK;
}