C 12
Graph.c Guest on 5th April 2021 10:48:48 AM
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. //#define VERTICES 7
  6.  
  7. typedef int __nodetype;
  8.  
  9. typedef struct node {
  10.     __nodetype key;
  11.     struct node *next;
  12. } NODE;
  13.  
  14. static unsigned int vertices;
  15.  
  16. int get_graph_size(char *argv[]) {
  17.   unsigned int size=0;
  18.   int a, b;
  19.   FILE *fp;
  20.   fp = fopen(argv[1],"r");
  21.   if (!fp) {
  22.     printf("Failed to open the file %s.\n",argv[1]);
  23.     return -1;
  24.   }
  25.   while (!feof(fp)) {
  26.     fscanf(fp,"%d%d", &a, &b);
  27.     size = a > size ? a : size;
  28.     size = b > size ? b : size;
  29.   }
  30.  
  31.   ++size;
  32.   printf("graph size: %d\n", size);
  33.  
  34.   return size;
  35. }
  36.  
  37. void insert_node(NODE *list[], int a, int b){
  38.     NODE* l = (NODE*)malloc(sizeof(NODE));
  39.     if (list[a] == NULL) {
  40.         l->key = b;
  41.         l->next = NULL;
  42.     } else {
  43.         l->key = b;
  44.         l->next = list[a];
  45.     }
  46.     list[a] = l;
  47. }
  48.  
  49. void print_adjacent_list(NODE *list[]){
  50.   int i;
  51.   NODE *tmp;
  52.   printf("\nPrinting adjacent list...\n\n");
  53.   for(i=0; i<vertices; i++) {
  54.     if (list[i]!=NULL) {
  55.       printf("(%d) ==> %d ", i, list[i]->key) ;
  56.       tmp = list[i]->next;
  57.       while (tmp != NULL) {
  58.         printf("==> %d  ", tmp->key);
  59.         tmp = tmp->next;
  60.       }
  61.     }
  62.     printf("\n");
  63.   }
  64. }
  65.  
  66. void print_adjacent_matrix(__nodetype matrix[vertices][vertices]) {
  67.   int i, j;
  68.   printf("\nPrinting adjacent matrix...\n\n");
  69.   for(i=0; i<vertices; i++) {
  70.     for(j=0; j<vertices; j++) {
  71.       printf("%d ", matrix[i][j]);
  72.     }
  73.     printf("\n");
  74.   }
  75. }
  76.  
  77. int create_adjacent_list(char *argv[], NODE *list[]) {
  78.   int i, a, b, prev_a, prev_b;
  79.   FILE *fp;
  80.   fp = fopen(argv[1],"r");
  81.  
  82.   for(i=0; i<vertices; i++)
  83.     list[i]=NULL;
  84.  
  85.   if (!fp) {
  86.     printf("Failed to open the file %s.\n",argv[1]);
  87.     return -1;
  88.   }
  89.  
  90.   while (!feof(fp)) {
  91.     fscanf(fp,"%d%d", &a, &b);
  92.     if (prev_a != a || prev_b != b) {
  93.           printf("a: %d, b: %d\n", a, b);
  94.       insert_node(list, a, b);
  95.     }
  96.     prev_a = a;
  97.     prev_b = b;
  98.   }
  99.   return 0;
  100. }
  101.  
  102. int create_adjacent_matrix(char *argv[], __nodetype matrix[vertices][vertices]) {
  103.   int i, j, a, b, prev_a, prev_b;
  104.   FILE *fp;
  105.   fp = fopen(argv[1],"r");
  106.  
  107.   for(i=0; i<vertices; i++)
  108.     for(j=0; j<vertices; j++)
  109.       matrix[i][j] = 0;
  110.  
  111.   if (!fp) {
  112.     printf("Failed to open the file %s.\n",argv[1]);
  113.     return -1;
  114.   }
  115.  
  116.   while (!feof(fp)) {
  117.     fscanf(fp,"%d%d", &a, &b);
  118.     if (prev_a != a || prev_b != b) {
  119.           printf("a: %d, b: %d\n", a, b);
  120.       matrix[a][b]=1;
  121.     }
  122.     prev_a = a;
  123.     prev_b = b;
  124.   }
  125.   return 0;
  126. }
  127.  
  128. int main(int argc, char *argv[]){
  129.  
  130.   vertices = get_graph_size(argv);
  131.  
  132.   NODE *adjacent_list[vertices];
  133.   __nodetype adjacent_matrix[vertices][vertices];
  134.  
  135.   create_adjacent_list(argv, adjacent_list);
  136.   print_adjacent_list(adjacent_list);
  137.  
  138.   create_adjacent_matrix(argv, adjacent_matrix);
  139.   print_adjacent_matrix(adjacent_matrix);
  140.  
  141.   return 0;
  142. }

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.