示例#1
0
/* ===============
 * Graph Functions
 * =============== */
Graph *newGraph(int nodes, int edges) 
{
   Graph *graph = malloc(sizeof(Graph));
   if(graph == NULL) 
   {
      print_to_log("Error (newGraph): malloc failure.\n");
      exit(1);
   }
   graph->nodes = makeNodeArray(nodes);
   graph->edges = makeEdgeArray(edges);

   graph->number_of_nodes = 0;
   graph->number_of_edges = 0;
   graph->root_nodes = NULL;
   return graph;
}
/*
 * The main() function first allocates memory on the heap for three arrays: nodes, (int) number of parents, (int) completed parents. It opens the file using fopen(), calls makeNodeArray() to create our data sctructure, and then calls scheduleNodes() to execute the instructions in the correct order. After that function returns, we free the memory allocated for the arrays and return.
 */
int main(int argc, char const *argv[]) {
  int nodeCount;
  FILE *input_file;
  node_t *nodes;
  int *number_parents;
  int *completed_parents;
  int i;
  // Allocate space for the Maximum number of nodes at the size of a node
  nodes = (node_t *) calloc(sizeof(node_t), MAX_NODES);
  number_parents = (int *) calloc(sizeof(int), MAX_NODES);
  completed_parents = (int *) calloc(sizeof(int), MAX_NODES);

  for (i = 0; i < MAX_NODES; i++) {
    number_parents[i] = 0;
    completed_parents[i] = UNEXECUTED;
  }
  // Get the filename
  char const *fname = argv[1];
  // Open the file
  input_file = fopen(fname, "r");
  if (!input_file) {
    perror("fopen failed to open the file");
  }
  if (input_file == 0) {
    perror("Cannot open file\n");
    exit(1);
  }
  // Make some nodes and put them in your array.
  nodeCount = makeNodeArray(nodes, input_file, number_parents);
  printf("This is my nodeCount: %d\n", nodeCount);

  fclose(input_file);
  // Execute the nodes in order.
  scheduleNodes(nodes, nodeCount, number_parents, completed_parents);
  // Release the memory so as to not have a memory leak
  free(nodes);
  free(number_parents);
  free(completed_parents);

  return 0;
}