How to do it...

Follow these steps to create an adjacency list representation of a graph:

  1. Define a structure called node that contains three members. One member, nme, is for storing the vertex of the graph; another member, vrt, is for connecting vertices of the graph; and, finally, edg is for connecting the adjacent vertices of the vertex.
  2. The user is asked to specify the count of the vertices in the graph.
  3. A linked list is created where the nme member of each node contains the vertex of the graph.
  4. All the nodes representing vertices of the graph are connected to each other using the vrt pointer.
  5. Once all the vertices are entered, the user is prompted to enter the edges of the graph. The user can enter any number of edges and to indicate that all the edges are entered, the user can enter 0 0 for the edge.
  6. When an edge is entered, for example, b, a temp1 pointer is used and is set to point to vertex a.
  7. A new node is created called newNode, and the vertex name b is assigned to the nme member of newNode.
  8. One more pointer is used, called temp2, and is set to point to the last node that is connected to vertex a. Once temp2 reaches the end of vertex a, the edg member of the temp2 node is set to point to newNode, and hence establishing an edge between a and b.

The program for creating the adjacency list representation of a directed graph is as follows:

//adjlistdirect.c

#include <stdlib.h>
#include <stdio.h>

struct node {
char nme;
struct node * vrt;
struct node * edg;
};

int main() {
int numb, i, j, noe;
char v1, v2;
struct node * startList, * newNode, * temp1, * temp2;
printf("How many vertices are there ? ");
scanf("%d", & numb);
startList = NULL;
printf("Enter all vertices names\n");
for (i = 1; i <= numb; i++) {
if (startList == NULL) {
newNode = malloc(sizeof(struct node));
scanf(" %c", & newNode - > nme); /* There is a space before %c */
startList = newNode;
temp1 = newNode;
newNode - > vrt = NULL;
newNode - > edg = NULL;
} else {
newNode = malloc(sizeof(struct node));
scanf(" %c", & newNode - > nme);
/* There is a space before %c */
newNode - > vrt = NULL;
newNode - > edg = NULL;
temp1 - > vrt = newNode;
temp1 = newNode;
}
}
printf("Enter the edges between vertices. Enter v1 v2, if there is an edge\n");
printf("between v1 and v2. Enter 0 0 if over\n");
noe = numb * (numb - 1);
for (j = 1; j <= noe; j++) {
scanf(" %c %c", & v1, & v2);
/* There is a space before %c */
if (v1 == '0' && v2 == '0') break;
temp1 = startList;
while (temp1 != NULL && temp1 - > nme != v1)
temp1 = temp1 - > vrt;
if (temp1 == NULL) {
printf("Sorry no vertex exist by this name\n");
break;
}
temp2 = temp1;
while (temp2 - > edg != NULL) temp2 = temp2 - > edg;
newNode = malloc(sizeof(struct node));
newNode - > nme = v2;
temp2 - > edg = newNode;
newNode - > edg = NULL;
newNode - > vrt = NULL;
}
printf("\nAdjacency List representation of Graph is\n");
temp1 = startList;
while (temp1 != NULL) {
printf("%c\t", temp1 - > nme);
temp2 = temp1 - > edg;
while (temp2 != NULL) {
printf("%c\t", temp2 - > nme);
temp2 = temp2 - > edg;
}
printf("\n");
temp1 = temp1 - > vrt;
}
}

Now, let's go behind the scenes to understand the code better.