home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / dev / c / graph next >
Encoding:
Text File  |  1992-07-21  |  6.9 KB  |  347 lines

  1. /*      > C.Graph - Graph data type */
  2.  
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "graph.h"
  7.  
  8. #ifdef test
  9. #include <stdio.h>
  10. #endif
  11.  
  12. /* Return values from functions */
  13.  
  14. #define OK      1
  15. #define ERR     0
  16.  
  17. /* Create an empty graph */
  18.  
  19. graph graph_new (int val_len, int weight_len)
  20. {
  21.         graph g;
  22.  
  23.         g = malloc( sizeof(struct graph) );
  24.  
  25.         if ( g == NULL )
  26.                 return NULL;
  27.  
  28.         g->nodes = NULL;
  29.  
  30.         return g;
  31. }
  32.  
  33. /* Create a new node, with value VALUE, and install it in graph G */
  34.  
  35. node graph_newnode (graph g, void *value)
  36. {
  37.         node n;
  38.         struct nodelist *p;
  39.  
  40.         n = malloc( sizeof(struct node) - 1 + g->val_len );
  41.  
  42.         if ( n == NULL )
  43.                 return NULL;
  44.  
  45.         p = malloc( sizeof(struct nodelist) );
  46.  
  47.         if ( p == NULL )
  48.         {
  49.                 free(n);
  50.                 return NULL;
  51.         }
  52.  
  53.         n->val_len  = g->val_len;
  54.         n->num_succ = 0;
  55.         n->num_pred = 0;
  56.         n->preds    = NULL;
  57.         n->succs    = NULL;
  58.         memcpy(n->value,value,g->val_len);
  59.  
  60.         p->node = n;
  61.         p->next = g->nodes;
  62.  
  63.         g->nodes = p;
  64.  
  65.         return n;
  66. }
  67.  
  68. /* Create a new arc, between nodes FROM and TO, with weight WEIGHT, and
  69.    install it in graph G */
  70.  
  71. arc graph_newarc (graph g, node from, node to, void *weight)
  72. {
  73.         arc a;
  74.         struct arclist *p1;
  75.         struct arclist *p2;
  76.  
  77.         a = malloc( sizeof(struct arc) - 1 + g->weight_len );
  78.  
  79.         if ( a == NULL )
  80.                 return NULL;
  81.  
  82.         p1 = malloc( sizeof(struct arclist) );
  83.  
  84.         if ( p1 == NULL )
  85.         {
  86.                 free(a);
  87.                 return NULL;
  88.         }
  89.  
  90.         p2 = malloc( sizeof(struct arclist) );
  91.  
  92.         if ( p2 == NULL )
  93.         {
  94.                 free(a);
  95.                 free(p1);
  96.                 return NULL;
  97.         }
  98.  
  99.         a->start = from;
  100.         a->end   = to;
  101.         memcpy(a->weight,weight,g->weight_len);
  102.  
  103.         p1->arc = a;
  104.         p2->arc = a;
  105.  
  106.         ++from->num_succ;
  107.         p1->next = from->succs;
  108.         from->succs = p1;
  109.  
  110.         ++to->num_pred;
  111.         p2->next = to->preds;
  112.         to->preds = p2;
  113.  
  114.         return a;
  115. }
  116.  
  117. /* Remove arc A from graph G */
  118.  
  119. void graph_freearc (graph g, arc a)
  120. {
  121.         node n;
  122.         struct arclist *p;
  123.         struct arclist *del;
  124.  
  125.         n = a->start;
  126.  
  127.         --n->num_succ;
  128.  
  129.         p = n->succs;
  130.  
  131.         if ( p->arc == a )
  132.         {
  133.                 del = p;
  134.                 n->succs = p->next;
  135.         }
  136.  
  137.         else
  138.         {
  139.                 for ( ; p->next != NULL; p = p->next )
  140.                 {
  141.                         if ( p->next->arc == a )
  142.                         {
  143.                                 del = p->next;
  144.                                 p->next = del->next;
  145.                                 break;
  146.                         }
  147.                 }
  148.         }
  149.  
  150.         free(del);
  151.  
  152.         n = a->end;
  153.  
  154.         --n->num_pred;
  155.  
  156.         p = n->preds;
  157.  
  158.         if ( p->arc == a )
  159.         {
  160.                 del = p;
  161.                 n->preds = p->next;
  162.         }
  163.  
  164.         else
  165.         {
  166.                 for ( ; p->next != NULL; p = p->next )
  167.                 {
  168.                         if ( p->next->arc == a )
  169.                         {
  170.                                 del = p->next;
  171.                                 p->next = del->next;
  172.                                 break;
  173.                         }
  174.                 }
  175.         }
  176.  
  177.         free(del);
  178.  
  179.         free(a);
  180. }
  181.  
  182. /* Free a node N from graph G */
  183.  
  184. int graph_freenode (graph g, node n)
  185. {
  186.         struct nodelist *p = g->nodes;
  187.         struct nodelist *del = NULL;
  188.         struct arclist *a;
  189.         struct arclist *temp;
  190.  
  191.         if ( p->node == n )
  192.         {
  193.                 del = p;
  194.                 g->nodes = p->next;
  195.         }
  196.         else
  197.         {
  198.                 for ( ; p->next != NULL; p = p->next )
  199.                 {
  200.                         if ( p->next->node == n )
  201.                         {
  202.                                 del = p->next;
  203.                                 p->next = del->next;
  204.                                 break;
  205.                         }
  206.                 }
  207.         }
  208.  
  209.         if ( del == NULL )
  210.                 return ERR;
  211.  
  212.         free(del);
  213.  
  214.         a = n->preds;
  215.  
  216.         while ( a != NULL )
  217.         {
  218.                 temp = a->next;
  219.                 graph_freearc(g,a->arc);
  220.                 free(a);
  221.                 a = temp;
  222.         }
  223.  
  224.         a = n->succs;
  225.  
  226.         while ( a != NULL )
  227.         {
  228.                 temp = a->next;
  229.                 graph_freearc(g,a->arc);
  230.                 free(a);
  231.                 a = temp;
  232.         }
  233.  
  234.         free(n);
  235.  
  236.         return OK;
  237. }
  238.  
  239. void graph_free (graph g)
  240. {
  241.         graph_clear(g);
  242.         free(g);
  243. }
  244.  
  245. void graph_clear (graph g)
  246. {
  247.         struct nodelist *p = g->nodes;
  248.  
  249.         while ( p != NULL )
  250.         {
  251.                 temp = p->next;
  252.                 graph_freenode(g,p->node);
  253.                 free(p);
  254.                 p = temp;
  255.         }
  256.  
  257.         g->nodes = NULL;
  258. }
  259.  
  260. int graph_copy (graph g1, graph g2)
  261. {
  262. }
  263.  
  264. int graph_equal (graph g1, graph g2)
  265. {
  266. }
  267.  
  268. int graph_empty (graph g)
  269. {
  270. }
  271.  
  272. int graph_size (graph g)
  273. {
  274. }
  275.  
  276. int graph_iterate (graph g, int (*process)(void *))
  277. {
  278. }
  279.  
  280. /* graph-specific routines */
  281.  
  282. /*---------------------------------------------------------------------------*/
  283.  
  284. #ifdef test
  285. int print (void *ptr)
  286. {
  287.         printf("%d ",*(int *)ptr);
  288.         return STATUS_CONTINUE;
  289. }
  290.  
  291. void graph_dump (graph g)
  292. {
  293.         printf("Graph: ");
  294.         graph_iterate(g,print);
  295.         putchar('\n');
  296. }
  297. #endif
  298.  
  299. /*---------------------------------------------------------------------------*/
  300.  
  301. #ifdef test
  302.  
  303. #define BUFLEN 255
  304.  
  305. int main (void)
  306. {
  307.         char buf[BUFLEN];
  308.         int i, j, num;
  309.         graph g[10];
  310.  
  311.         for ( i = 0; i < 10; ++i )
  312.                 g[i] = graph_new(sizeof(int));
  313.  
  314.         for ( ; ; )
  315.         {
  316.                 printf(">");
  317.                 fgets(buf,BUFLEN,stdin);
  318.  
  319.                 if ( buf[0] == '\n' || buf[0] == '\0' )
  320.                         continue;
  321.                 else if ( sscanf(buf,"clear %1d",&i) == 1 )
  322.                         graph_clear(g[i]);
  323.                 else if ( sscanf(buf,"copy %1d %1d",&i,&j) == 2 )
  324.                         graph_copy(g[i],g[j]);
  325.                 else if ( sscanf(buf,"equal %1d %1d",&i,&j) == 2 )
  326.                         printf("%s\n",(graph_equal(g[i],g[j]) ? "yes" : "no"));
  327.                 else if ( sscanf(buf,"empty %1d",&i) == 1 )
  328.                         printf("%s\n",(graph_empty(g[i]) ? "yes" : "no"));
  329.                 else if ( sscanf(buf,"size %1d",&i) == 1 )
  330.                         printf("%d\n",graph_size(g[i]));
  331.                 else if ( sscanf(buf,"dump %1d",&i) == 1 )
  332.                         graph_dump(g[i]);
  333.                 else if ( strncmp(buf,"quit",4) == 0 )
  334.                         break;
  335.                 else
  336.                         printf("Mistake\n");
  337.         }
  338.  
  339.         printf("Deleting g[0-9]\n");
  340.         for ( i = 0; i < 10; ++i )
  341.                 graph_free(g[i]);
  342.  
  343.         return 0;
  344. }
  345.  
  346. #endif
  347.