A C Program to demonstrate adjacency list representation of graphs
w

Graph and its representations - GeeksforGeeks
http://www.geeksforgeeks.org/graph-and-its-representations/
// A C Program to demonstrate adjacency list representation of graphs #include <stdio.h>
#include <stdlib.h> // A structure to represent an adjacency list node
struct AdjListNode {
int dest;
struct AdjListNode* next;
}; // A structure to represent an adjacency list
struct AdjList {
struct AdjListNode *head; // pointer to head node of list
}; // A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph {
int V;
struct AdjList* array;
}; // A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
} // A utility function that creates a graph of V vertices
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V; // Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); // Initialize each adjacency list as empty by making head as NULL
int i;
for (i = ; i < V; ++i)
graph->array[i].head = NULL; return graph;
} // Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest) {
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode; // Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
} // A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph) {
int v;
for (v = ; v < graph->V; ++v) {
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl) {
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
} // Driver program to test above functions
int main() {
// create the graph given in above fugure
int V = ;
struct Graph* graph = createGraph(V);
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , ); // print the adjacency list representation of the above graph
printGraph(graph); return ;
}
A C Program to demonstrate adjacency list representation of graphs的更多相关文章
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- Graph I - Graph
Graph There are two standard ways to represent a graph G=(V,E)G=(V,E), where VV is a set of vertices ...
- 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)
wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- class Solution(object): def fizzBuzz(self, n): a = [] i = 1 while(i <= n): if(i%15 == 0): a.append("FizzBuzz") elifleetcode day_01
412. Fizz Buzz Write a program that outputs the string representation of numbers from 1 to n. But fo ...
- LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...
随机推荐
- 01-Hibernate Tools for Eclipse Plugins安装
Hibernate Tools for Eclipse Plugins安装 在线安装有两种方法 方法一:"Help > Install New Software Updates&quo ...
- acm之路--母函数 by小宇
母函数又叫生成函数,原是数学上的一个名词,是组合数学中的一个重要理论. 生成函数是说,构造这么一个多项式函数g(x).使得x的n次方系数为f(n). 对于母函数,看到最多的是这样两句话: 1.&quo ...
- Python 的条件语句和循环语句
一.顺序结构 顺序结构是最简单的一种程序结构,程序按照语句的书写次序自上而下顺序执行. 二.分支控制语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块 ...
- 在CMD中查看端口被什么程序占用
我们要查看端口被什么程序占用,可以使用下面方法.比如端口28848 1. 打开cmd,输入命令netstat -ano | findstr ":28848",显示结果如下,最后一个 ...
- Math函数的"四舍五入",Floor,Ceiling,Round的一些注意事项!
1.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0Math.Round(0.1) //0Math.Round(0.2) //0Math.Round(0.3) / ...
- 可以开发着玩一下的web项目
博客项目:发布博客,写博客 车辆.车队管理系统 教师评价系统 仓储管理系统 进销存管理系统 客户管理系统 结算系统 医院病历管理系统
- window安装mysql
window安装mysql 1. 官网下载mysql zip安装包,然后解压到你想安装的目录,假设解压的目录是P:\mysql 解压完的目录:bin docs include lib share CO ...
- Python 安装 MaxMind GeoLite City
1.先安装 geoip c library geoip c library >= 1.4.6 installed on your machine. >= 1.4.6 installed ...
- mysql 1005 错误
建立外键的时候两个 表的相对应的 类型不一致!
- Matlab字符串分割
data = '1.21, 1.985, 1.955, 2.015, 1.885'; C = strsplit(data,', ') C = '1.21' '1.985' '1.955' '2.015 ...