0% found this document useful (0 votes)
35 views1 page

Usmereni Graf

This C program defines structs for nodes and graphs to represent a graph as an adjacency list. It includes functions to create a graph from an edge list and print the graph. The main function tests it by creating a graph from an example edge list and printing the graph.

Uploaded by

Ilhan Basic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views1 page

Usmereni Graf

This C program defines structs for nodes and graphs to represent a graph as an adjacency list. It includes functions to create a graph from an edge list and print the graph. The main function tests it by creating a graph from an example edge list and printing the graph.

Uploaded by

Ilhan Basic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
#define N 6

typedef struct Node{


int dest;
struct Node*next;
}Node;

typedef struct Graph{


struct Node*head[N];
}Graph;

typedef struct Edge{


int src,dest;
}Edge;

Graph*createGraph(Edge edges[],int n){


int i;
Graph* graph= (Graph*)malloc(sizeof(Graph));
for(i=0;i<N;i++)
graph->head[i]=NULL;
for(i=0;i<n;i++){
int src=edges[i].src;
int dest=edges[i].dest;
Node*newNode=(Node*)malloc(sizeof(Node));
newNode->dest=dest;
newNode->next=graph->head[src];
graph->head[src]=newNode;
}
return graph;
}
void printGraph(Graph* graph){
int i;
for(i=0;i<N;i++){
Node*ptr=graph->head[i];
while(ptr!=NULL){
printf("%d ",ptr->dest);
ptr=ptr->next;
}
printf("\n");
}
}
int main(){
Edge edges[]=
{
{0,1},{1,2},{2,0},{2,1},{3,2},{4,5},{5,4}
};
int n=sizeof(edges)/sizeof(edges[0]);
Graph*graph=createGraph(edges,n);
printGraph(graph);
return 0;
}

You might also like