Exemplo n.º 1
0
int readStationListFromFile(FILE *f, JRB stationList) {
	String temp;
	StationInfo *station;
	int n = 0;
	
	printf("Station list: \n");
	rewind(f);
	fgets(temp.content, SIZE, f);
	while(fscanf(f, "%[^=]=", temp.content) != EOF) {
		if(strstr(temp.content, "[LINES]") != NULL) {
			break;
		}

		if((station = myMalloc(sizeof(StationInfo), 1)) == NULL) {
			return -1;
		}

		strcpy(station->code.content, temp.content);
		fgets(temp.content, SIZE, f);
		temp.content[strlen(temp.content) - 1] = '\0';
		strcpy(station->name.content, temp.content);

		jrb_insert_gen(stationList, new_jval_v(&station->name), new_jval_v(&station->code), stringCompare);

		printf("Station name: \"%s\" - \"%s\"\n", station->name.content, station->code.content);
	}
	return n;
}
Exemplo n.º 2
0
int insertEdge(Graph g, Jval v1, Jval v2,
				int (*cmp)(Jval, Jval))
{
	Graph found;
	Jval j_adj_list;
	Jval j = new_jval_v(NULL);
	found = jrb_find_gen(g, v1, cmp);

	if(found == NULL){
		/* if not found
		 create a new adjacent list which v2 is added to
		 then insert the adjacent list to the tree
		 with the key is v1
		*/

		 // create a new adj_list
		JRB adj_list = make_jrb();
		jrb_insert_gen(adj_list, v2, j, cmp);
		j_adj_list = new_jval_v(adj_list);

		// add adjacent list to the tree
		jrb_insert_gen(g, v1, j_adj_list, cmp);

		return 0;

	}else{
		/*
		if found
		check if v2 is in the adj_list of v1
				if not, add v2 to adj_list of v1
				else return status code 1
		*/

		JRB adj_list = (JRB) jval_v(found->val);
		found = jrb_find_gen(adj_list, v2, cmp);
		if(found == NULL){
			jrb_insert_gen(adj_list, v2, j, cmp);
			return 0;
		}else return 1;
	}
}
Exemplo n.º 3
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;
}
Exemplo n.º 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]);
				}
			}
		}
	}
}
Exemplo n.º 5
0
int BFStraverse(Graph graph, Jval start,
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	
	Dllist node, queue;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n, counter = 0;

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

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

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

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			counter++;
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			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 counter;
}