/* * C++ Program to Describe the Representation of Graph using Adjacency List */ #include #include #include using namespace std; int c = 0; struct adj_list { int dest; adj_list *next; }*np = NULL, *np1 = NULL, *p = NULL, *q = NULL; struct Graph { int v; adj_list *ptr; } array[5]; void addReverseEdge(int src,int dest) { np1 = new adj_list; np1->dest = src; np1->next = NULL; if (array[dest].ptr == NULL) { array[dest].ptr = np1; q = array[dest].ptr; q->next = NULL; } else { q = array[dest].ptr; while (q->next != NULL) { q = q->next; } q->next = np1; } } void addEdge(int src,int dest) { np = new adj_list; np->dest = dest; np->next = NULL; if (array[src].ptr == NULL) { array[src].ptr = np; p = array[src].ptr; p->next = NULL; } else { p = array[src].ptr; while (p->next != NULL) { p = p->next; } p->next = np; } addReverseEdge(src,dest); } void print_graph(int n) { for (int i = 0; i < n; i++) { cout<<"Adjacency List of "<dest<<"\t"; array[i].ptr = (array[i].ptr)->next; } cout<>n; for (int i = 0; i < n; i++) { array[i].v = i; array[i].ptr = NULL; } addEdge(0, 1); addEdge(0, 4); addEdge(1, 2); addEdge(1, 3); addEdge(1, 4); addEdge(2, 3); addEdge(3, 4); print_graph(n); getch(); } /* Enter the no of vertices 5 Adjacency List of 0 1 4 Adjacency List of 1 0 2 3 4 Adjacency List of 2 1 3 Adjacency List of 3 1 2 4 Adjacency List of 4 0 1 3