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的更多相关文章

  1. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  2. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  3. 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 ...

  4. 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  5. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  6. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  7. LeetCode 412. Fizz Buzz

    Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...

  8. 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 ...

  9. LeetCode Fizz Buzz

    原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...

随机推荐

  1. 【DB2】经典SQL写法

    1.环境准备 CREATE TABLE DataInfo( ID_1 ), ID_2 ) ) INSERT INTO DataInfo VALUES('A','Oracle'); INSERT INT ...

  2. Oracle 存储过程调用返回游标的另一个存储过程。

    一个扩展存储过程调用另一个存储过程,示例: 被调用存储过程:最后会返回一个游标,游标返回一个值.调用这个存储过程的存储过程同样需要获取它. procedure SearchBill --根据到货单号查 ...

  3. <转>程序员的心理疾病

    注:本文转自大神王垠的博客 原文出处 http://www.yinwang.org/blog-cn/2014/02/09/programmer-mental/ 说实话,虽然似乎为之奋斗了十多年,在真正 ...

  4. linq-to-sql实现left join,group by,count

    linq-to-sql实现left join,group by,count 用linq-to-sql实现下面的sql语句: SELECT p.ParentId, COUNT(c.ChildId) FR ...

  5. PyCharm 环境配置

    1.去掉“自动保存功能” pycharm默认是自动保存的,习惯自己按 ctrl + s 的可以进行如下设置: 菜单File -> Settings... -> Appearance &am ...

  6. LVM详解笔记pv-vg-lv创建和扩展

    LVM Logical Volume Manager(逻辑卷管理) 是Linux环境下对底层磁盘的一种管理机制(方式),处在物理磁盘和文件系统之间. 名词: PV (Physical Volume)物 ...

  7. mongodb 实现关系型数据库中查询某一列 的效果

    近期在tornado\mongodb\ansible mongodb中有个find()方法非常牛逼,能够将集合中全部的表都传出来,一開始我这么写 class Module_actionHandler( ...

  8. C语言中fgetc函数返回值为什么是int?

    学习C语言的,文件操作,大都会用到它. 它的函数原型: 这个函数的返回值,是返回读取的一个字节.如果读到文件末尾返回EOF.EOF其实就是一个宏#define EOF (-1)表示-1.既然返回的是一 ...

  9. C语言基础(17)-作用域

    一个C语言变量的作用域可以是代码块 作用域,函数作用域或者文件作用域. 不推荐写法 int a; // 出现了语法的二义性,可能是声明也可能是定义,所以最好定义完成之后声明 void func();  ...

  10. 深入浅出--iOS的TCP/IP协议族剖析&&Socket

    深入浅出--iOS的TCP/IP协议族剖析&&Socket   简介 该篇文章主要回顾--TCP/IP协议族中的TCP/UDP.HTTP:还有Socket.(--该文很干,酝酿了许久! ...