Don't like ads? PRO users don't see any ads ;-)
Guest

functions

By: a guest on May 2nd, 2012  |  syntax: C  |  size: 2.98 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "graph.h"
  2.  
  3. int initialize_graph (Graph *mygraph, int MaxSize) {
  4.   // your code goes here
  5.   mygraph->MaxSize = MaxSize;
  6.   mygraph->table=(Node*)malloc(sizeof(Node)*MaxSize);
  7.   check(mygraph->table,"Fine");
  8.   return 0;
  9. }
  10. int insert_graph_node (Graph *mygraph, int n, char *name) {
  11.   // your code goes here
  12.   mygraph->table[n].name=name;
  13.   mygraph->table[n].outdegree=0;
  14.   mygraph->table[n].outlist=NULL;
  15.   return 0;
  16. }
  17. int insert_graph_link (Graph *mygraph, int source, int target) {
  18.   // your code goes here
  19.   List* nList = malloc(sizeof(List));
  20.   nList->index = target;
  21.   nList->next=NULL;
  22.   List* aux = mygraph->table[source].outlist;
  23.   if (mygraph->table[source].outdegree==0)
  24.     mygraph->table[source].outlist=nList;
  25.   else
  26.   {
  27.     while(mygraph->table[source].outlist->next!=NULL)
  28.     {
  29.       mygraph->table[source].outlist =
  30.         mygraph->table[source].outlist->next;
  31.     }
  32.     mygraph->table[source].outlist=Nlist;
  33.     mygraph->table[source].outlist=aux;
  34.   }
  35.   mygraph->table[source].outdegree++;
  36.   return 0;    
  37. }
  38. // use to check result of strdup, malloc etc.
  39. void check (void *memory, char *message) {
  40.   if (memory == NULL) {
  41.     fprintf (stderr, "Can't allocate memory for %s\n", message);
  42.     exit (3);
  43.   }
  44. }
  45. int read_graph (Graph *mygraph, char *filename)
  46. /*
  47.  * Reads in graph from FILE *filename which is of .gx format.
  48.  * Stores it as Graph in *mygraph.
  49.  * Returns an error if file does not start with MAX command,
  50.  * or if any subsequent line is not a NODE or EDGE command.
  51.  * Does not check that node numbers do not exceed the maximum number
  52.  * Defined by the MAX command.
  53.  * 8/2/2010 - JLS
  54.  */
  55. {
  56.   FILE *fp;
  57.   char command[80], name[80];
  58.   int i, s, t;
  59.   fp= fopen (filename, "r");
  60.   if (fp==NULL) {
  61.     fprintf(stderr,"cannot open file %s\n", filename);
  62.     return -1;
  63.   }
  64.   printf ("Reading graph from %s\n", filename);
  65.   fscanf (fp,"%s", command);
  66.   if (strcmp (command, "MAX")!=0) {
  67.     fprintf (stderr, "Error in graphics file format\n");
  68.     return -1;
  69.   } else {
  70.     fscanf (fp, "%d", &i);
  71.     initialize_graph (mygraph, i+1); // +1 so nodes can be numbered 1..MAX
  72.     while (fscanf (fp, "%s", command)!=EOF) {
  73.       if (strcmp (command, "NODE")==0) {
  74.         fscanf (fp, "%d %s", &i, name);
  75.         insert_graph_node (mygraph, i, name);
  76.       } else if (strcmp (command, "EDGE")==0) {
  77.         fscanf (fp, "%d %d", &s, &t);
  78.         insert_graph_link (mygraph, s, t);
  79.       } else return -1;
  80.     }
  81.   }
  82.   return 0;
  83. }
  84. void print_graph (Graph *mygraph)
  85. /*
  86.  * Prints out Graph *mygraph to the stdout in .gx format.
  87.  * 8/2/2010 - JLS
  88.  */
  89. {
  90.   int i;
  91.   List *current;
  92.   printf ("MAX %d\n", mygraph->MaxSize);
  93.   for (i=0; i<mygraph->MaxSize; i++)
  94.     if (mygraph->table[i].name!=NULL) {
  95.       printf ("NODE %d %s\n", i, mygraph->table[i].name);
  96.       current= mygraph->table[i].outlist;
  97.       while (current!=NULL) {
  98.         printf ("EDGE %d %d\n", i, current->index);
  99.         current= current->next;
  100.       }
  101.     }
  102. }