#include <stdio.h>
#include "windows.h"
#define MaxVertex 5
typedef char E;
typedef struct Node{
int edge;
struct Node *next;
}* node;
typedef struct Head{
E element;
struct Node * next;
}* head;
typedef struct matrixGraph {
int vertexCount, edgeCount;
struct Head vertex[MaxVertex];
} *Graph;
Graph create() {
Graph graph = malloc(sizeof(struct matrixGraph));
graph->vertexCount = graph->edgeCount = 0;
return graph;
}
void addVertex(Graph graph, E element) {
if (graph->vertexCount != MaxVertex) {
graph->vertex[graph->vertexCount].element = element;
graph->vertex[graph->vertexCount++].next = NULL;
}
}
void addEdge(Graph graph, int a, int b) {
node tmp = graph->vertex[a].next;
node E = malloc(sizeof (struct Node));
E->edge = b;
E->next = NULL;
while (tmp){
if (tmp->edge == b) return;
if (!tmp->next){
tmp->next = E;
graph->edgeCount++;
return;
}
tmp = tmp->next;
}
graph->vertex[a].next = E;
graph->edgeCount++;
}
void f(Graph graph){
for (int i = 0; i < graph->vertexCount; ++i) {
printf("%d | %c",i,graph->vertex[i].element);
node tmp = graph->vertex[i].next;
while (tmp){
printf("-> %d",tmp->edge);
tmp = tmp->next;
}
printf("\n");
}
}
int main(){
Graph graph = create();
addVertex(graph,'A');
addVertex(graph,'B');
addVertex(graph,'C');
addVertex(graph,'D');
addEdge(graph,0,1);
addEdge(graph,1,2);
addEdge(graph,2,3);
addEdge(graph,3,0);
addEdge(graph,2,0);
f(graph);
}