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 ...
随机推荐
- matlab经常使用小函数(一)
(第1维为对每一列操作.第2维维对每一行操作) sum 求和操作 max 求最大值操作 sum:求和操作 sum(A):矩阵A按列向求和(每一列求和).结果为一个行向量 sum(A,2):矩阵 ...
- 【Android Studio探索之路系列】之中的一个:Android Studio开篇
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...
- (五)Oracle学习笔记—— 视图
1. 视图简介 视图是虚表,没有具体物理数据,是通过实体表的一种计算映射逻辑.主要就是为了方便和数据安全. 2. 视图作用 简化数据操作:视图可以简化用户处理数据的方式. 着重于特定数据:不必要的数据 ...
- c++ vector容器的使用,序列倒叙reverse(),容器底部插入一个数值push_back()
问题:程序实现将que[i]添加到que2最后,再将que2反转输出. 例如: 输入 4 1 2 3 4 输出 4 2 1 3 #include<iostream> #include< ...
- Linux 时间修改--date -s命令
Linux 时间修改 不重启修改时区 一.修改linux的时间root使用date指令:date -s1.只修改日期,不修改时间,输入:Linux代码 1. date -s 2007-08-03 da ...
- linux-c 调试 gdb
GDB(GNU Debugger) gcc -g –o testarg testarg.c //可执行文件中带上调试信息,用于后续的gdb调试 gdb testarg l; list //显示源程序 ...
- linux 进入 GNOME X 界面
CentOS 安装Gnome CentOSVmwareLinuxBlogHTML 刚开始装系统的时候,没有选Gnome或者KDE,现在想装个玩玩. 简单的安装可以参考这个:http://huruxi ...
- unity, Invoke延迟执行
参考:http://blog.christianhenschel.com/2013/05/15/how-to-do-delayed-function-calls-in-unity3d-one-line ...
- Python学习之格式符
%s 字符串 (采用str()的显示) %r 字符串 (采用repr()的显示) %c 单个字符 %b 二进制整数 %d 十进制整数 %i 十进制整数 %o ...
- spring 第一篇(1-3):鸟瞰spring蓝图
如你所见,spring框架的核心是关注于如何使用DI.AOP和模板来让企业级java开发变得更简单.spring确实也是这样做的,所以很值得你去使用它.不过spring内容可能比你所能看到的要多很多. ...