Graph Representation using Linked List in C

This is a C Program to generates graph using Linked List Method. In this representation, the n rows of the adjacency matrix are represented as n linked lists. There is one list for each vertex in G. The nodes in list i represent the vertices that are adjacent from vertex i. Each node has at least two fields : vertex and next.

Here is source code of the C Program to Represent Graph Using Linked List. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define new_node (struct node*)malloc(sizeof(struct node))
  4. struct node {
  5.     int vertex;
  6.     struct node *next;
  7. };
  8. void main() {
  9.     int option;
  10.     do {
  11.         printf(
  12.                 "\n A Program to represent a Graph by using an Linked List \n ");
  13.         printf("\n 1. Directed Graph ");
  14.         printf("\n 2. Un-Directed Graph ");
  15.         printf("\n 3. Exit ");
  16.         printf("\n\n Select a proper option : ");
  17.         scanf("%d", &option);
  18.         switch (option) {
  19.             case 1:
  20.                 dir_graph();
  21.                 break;
  22.             case 2:
  23.                 undir_graph();
  24.                 break;
  25.             case 3:
  26.                 exit(0);
  27.         }
  28.     } while (1);
  29. }
  30. int dir_graph() {
  31.     struct node *adj_list[10], *p;
  32.     int n;
  33.     int in_deg, out_deg, i, j;
  34.     printf("\n How Many Vertices ? : ");
  35.     scanf("%d", &n);
  36.     for (i = 1; i <= n; i++)
  37.         adj_list[i] = NULL;
  38.     read_graph(adj_list, n);
  39.     printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
  40.     for (i = 1; i <= n; i++) {
  41.         in_deg = out_deg = 0;
  42.         p = adj_list[i];
  43.         while (p != NULL) {
  44.             out_deg++;
  45.             p = p -> next;
  46.         }
  47.         for (j = 1; j <= n; j++) {
  48.             p = adj_list[j];
  49.             while (p != NULL) {
  50.                 if (p -> vertex == i)
  51.                     in_deg++;
  52.                 p = p -> next;
  53.             }
  54.         }
  55.         printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n", i, in_deg, out_deg,
  56.                 in_deg + out_deg);
  57.     }
  58.     return;
  59. }
  60. int undir_graph() {
  61.     struct node *adj_list[10], *p;
  62.     int deg, i, j, n;
  63.     printf("\n How Many Vertices ? : ");
  64.     scanf("%d", &n);
  65.     for (i = 1; i <= n; i++)
  66.         adj_list[i] = NULL;
  67.     read_graph(adj_list, n);
  68.     printf("\n Vertex \t Degree ");
  69.     for (i = 1; i <= n; i++) {
  70.         deg = 0;
  71.         p = adj_list[i];
  72.         while (p != NULL) {
  73.             deg++;
  74.             p = p -> next;
  75.         }
  76.         printf("\n\n %5d \t\t %d\n\n", i, deg);
  77.     }
  78.     return;
  79. }
  80. int read_graph(struct node *adj_list[10], int n) {
  81.     int i, j;
  82.     char reply;
  83.     struct node *p, *c;
  84.     for (i = 1; i <= n; i++) {
  85.         for (j = 1; j <= n; j++) {
  86.             if (i == j)
  87.                 continue;
  88.             printf("\n Vertices %d & %d are Adjacent ? (Y/N) :", i, j);
  89.             scanf("%c", &reply);
  90.             if (reply == 'y' || reply == 'Y') {
  91.                 c = new_node;
  92.                 c -> vertex = j;
  93.                 c -> next = NULL;
  94.                 if (adj_list[i] == NULL)
  95.                     adj_list[i] = c;
  96.                 else {
  97.                     p = adj_list[i];
  98.                     while (p -> next != NULL)
  99.                         p = p -> next;
  100.                     p -> next = c;
  101.                 }
  102.             }
  103.         }
  104.     }
  105.     return;
  106. }

Output:

$ gcc GraphUsingLinkedLIst.c
$ ./a.out
 
A Program to represent a Graph by using an Liniked Matrix method 
 
 1. Directed Graph 
 2. Un-Directed Graph 
 3. Exit 
 
 Select a proper option : 
 How Many Vertices ? : 
 Vertices 1 & 2 are Adjacent ? (Y/N) : N
 Vertices 1 & 3 are Adjacent ? (Y/N) : Y
 Vertices 1 & 4 are Adjacent ? (Y/N) : Y
 Vertices 2 & 1 are Adjacent ? (Y/N) : Y
 Vertices 2 & 3 are Adjacent ? (Y/N) : Y
 Vertices 2 & 4 are Adjacent ? (Y/N) : N
 Vertices 3 & 1 are Adjacent ? (Y/N) : Y
 Vertices 3 & 2 are Adjacent ? (Y/N) : Y
 Vertices 3 & 4 are Adjacent ? (Y/N) : Y
 Vertices 4 & 1 are Adjacent ? (Y/N) : Y
 Vertices 4 & 2 are Adjacent ? (Y/N) : N
 Vertices 4 & 3 are Adjacent ? (Y/N) : Y
 Vertex 	 In_Degree 	Out_Degree	  Total_Degree  
     1			2			0				2
     2			1			2				3
     3			0			1				1
     4			1			1				2
 
 
 A Program to represent a Graph by using an Adjacency Matrix method 
 
 1. Directed Graph 
 2. Un-Directed Graph 
 3. Exit

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.