-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_comp.c
59 lines (51 loc) · 1.26 KB
/
graph_comp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>
#include<stdlib.h>
#include"header.h"
#include"cc.h"
struct node* createnode(int vertex_no)
{
struct node* ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->vertex=vertex_no;
ptr->next=NULL;
return ptr;
}
struct graph* creategraph(int no_vertices)
{
struct graph* graph;
graph = (struct graph*)malloc(sizeof(struct graph));
graph->no_vertices=no_vertices;
graph->adj_list= (struct node**)malloc(no_vertices*sizeof(struct node*));
for(int i=0;i<no_vertices;i++)
{
graph->adj_list[i]=NULL;
}
return graph;
}
void add_edge(struct graph* graph,int ver_1,int ver_2)
{
struct node* new_node;
new_node = createnode(ver_2);
new_node->next = graph->adj_list[ver_1];
graph->adj_list[ver_1] = new_node;
new_node = createnode(ver_1);
new_node->next = graph->adj_list[ver_2];
graph->adj_list[ver_2] = new_node;
}
void print_graph(struct graph* graph)
{
struct node* ptr;
printf("\nGRAPH -\n no.of vertices : %d",graph->no_vertices);
printf("\n\nADJ LIST -\n");
for(int i=0;i<graph->no_vertices;i++)
{
ptr = graph->adj_list[i];
printf(" %d -",i);
while(ptr!=NULL)
{
printf(" %d, ",ptr->vertex);
ptr=ptr->next;
}
printf("\n");
}
}