Rewriting: Change the Go Code to C

Could be :

	#include <stdbool.h> 
	#include <stdlib.h> 

	struct _item {
		int vertex;	
	};

	struct graph {
		int size;
		struct _item**adjList;
	};

	int* topoSort (struct graph* g ){
		int* result = (int*) malloc(g->size*sizeof(int));
		bool* marks = (bool*) malloc(g->size*sizeof(bool));
		int resultIndex = g->size-1;

		void visit(int u){
			for (int i;i<(sizeof g->adjList[u] / sizeof g->adjList[u][0]);i++) {
				struct _item item = g->adjList[u][i];
				if (!marks[item.vertex]) {
					visit(item.vertex);

				}
			}
			marks[u]=true;
			result[resultIndex] = u;
			resultIndex--;
		}
		for (int u;u<(sizeof g->adjList / sizeof g->adjList[0]);u++) {
			if (!marks[u]) {
				visit(u);
			}
		}

		return result;

	}
1 Like