I haven't yet implemented any sort of graph thus far in C and decided to give it a go by trying to implement an adjacency list in C. Is there anything in my code that you see that I can improve and is there any other sort of basic functionality that is missing in my adjacency list and should be added?
Graph.h
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
typedef struct AdjListNode AdjListNode_t;
typedef struct AdjList AdjList_t;
typedef enum {UNDIRECTED = 0, DIRECTED} Edge_t;
typedef struct Graph
{
Edge_t typeOfGraph;
int totalVertices;
int totalEdges;
AdjList_t *adjListArr;
}Graph_t;
AdjListNode_t *createAdjacentNode(int);
Graph_t *createGraph(int, Edge_t);
void deleteGraph(Graph_t *);
void addEdge(Graph_t *, int, int);
int doesEdgeExist(Graph_t *, int, int);
int calculateNumOfEdges(int, Edge_t);
void displayGraph(Graph_t *);
#endif
Graph.c
#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
struct AdjListNode
{
int vertex;
struct AdjListNode *next;
};
struct AdjList
{
int totalMembers;
struct AdjListNode *head;
};
AdjListNode_t *createAdjacentNode(int vertex)
{
AdjListNode_t *newNode = (AdjListNode_t *)malloc(sizeof(AdjListNode_t));
if(newNode == NULL)
{
return NULL;
}
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
Graph_t *createGraph(int totalVertices, Edge_t typeOfGraph)
{
int i;
Graph_t *graph = (Graph_t *)malloc(sizeof(Graph_t));
if(graph == NULL)
{
return NULL;
}
graph->totalVertices = totalVertices;
graph->totalEdges = 0;
graph->typeOfGraph = typeOfGraph;
graph->adjListArr = (AdjList_t *)malloc(totalVertices * sizeof(AdjList_t));
if(graph->adjListArr == NULL)
{
free(graph);
return NULL;
}
for(i = 0; i < totalVertices; i++)
{
graph->adjListArr[i].head = NULL;
graph->adjListArr[i].totalMembers = 0;
}
return graph;
}
void deleteGraph(Graph_t *graph)
{
if(graph != NULL)
{
if(graph->adjListArr != NULL)
{
int vertex;
for(vertex = 0; vertex < graph->totalVertices; vertex++)
{
AdjListNode_t *listIterator = graph->adjListArr[vertex].head;
while(listIterator != NULL)
{
AdjListNode_t *temp = listIterator;
listIterator = listIterator->next;
free(temp);
}
}
free(graph->adjListArr);
}
free(graph);
}
}
void addEdge(Graph_t *graph, int src, int dest)
{
if((src >= graph->totalVertices || src < 0) || (dest >= graph->totalVertices || dest < 0))
return;
if(doesEdgeExist(graph, src, dest))
return;
AdjListNode_t *newNode = createAdjacentNode(dest);
if(newNode != NULL)
{
newNode->next = graph->adjListArr[src].head;
graph->adjListArr[src].head = newNode;
graph->adjListArr[src].totalMembers++;
graph->totalEdges++;
if(graph->typeOfGraph == UNDIRECTED)
{
newNode = createAdjacentNode(src);
if(newNode != NULL)
{
newNode->next = graph->adjListArr[dest].head;
graph->adjListArr[dest].head = newNode;
graph->adjListArr[dest].totalMembers++;
graph->totalEdges++;
}
}
}
}
int doesEdgeExist(Graph_t *graph, int src, int dest)
{
AdjListNode_t *srcVertexPtr = graph->adjListArr[src].head;
while(srcVertexPtr != NULL)
{
if(srcVertexPtr->vertex == dest)
{
return 1;
}
else
srcVertexPtr = srcVertexPtr->next;
}
return 0;
}
int calculateNumOfEdges(int totalNumberOfEdges, Edge_t typeOfGraph)
{
/*
I'm assuming the graph has no self loops or multi-edges.
*/
if(typeOfGraph == UNDIRECTED)
{
return totalNumberOfEdges / 2;
}
else
return totalNumberOfEdges;
}
void displayGraph(Graph_t *graph)
{
int vertex;
for(vertex = 0; vertex < graph->totalVertices; vertex++)
{
AdjListNode_t *listIterator = graph->adjListArr[vertex].head;
printf("Vertex %d is adjacent to ", vertex);
while(listIterator != NULL)
{
printf("%d->", listIterator->vertex);
listIterator = listIterator->next;
}
printf("NULL\n");
}
}
Main.c
#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
int main()
{
Graph_t *uGraph = createGraph(5, UNDIRECTED);
if(uGraph == NULL)
{
printf("Couldn't not allocate any memory. Terminating Program. ");
exit(EXIT_FAILURE);
}
addEdge(uGraph, 0, 1);
addEdge(uGraph, 0, 4);
addEdge(uGraph, 0, 2);
addEdge(uGraph, 1, 2);
addEdge(uGraph, 1, 3);
addEdge(uGraph, 1, 4);
addEdge(uGraph, 2, 3);
addEdge(uGraph, 3, 4);
printf("Undirected Graph\n\n");
displayGraph(uGraph);
printf("\nThe total number edges in this graph is %d\n",
calculateNumOfEdges(uGraph->totalEdges, uGraph->typeOfGraph));
deleteGraph(uGraph);
return 0;
}
Output
