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. TCP和UDP比較

    一.TCP/IP协议 TCP/IP协议,你一定常常听说吧,当中TCP(Transmission Control Protocol)称为传输控制协议,IP(Internet Protocol)称为因特网 ...

  2. WPF中Auto与*的差别

    Auto 表示自己主动适应显示内容的宽度, 如自己主动适应文本的宽度,文本有多长,控件就显示多长. * 则表示按比例来分配宽度. <ColumnDefinition Width="3* ...

  3. 在VS2013 IIS Express 添加MIME映射

    打开VS2013返回json提示MIME映射问题 1.在DOS窗口下进入IIS Express安装目录,默认是C:\Program Files\IIS Express,cmd  命令行cd 到 该目录 ...

  4. mysql数据库初始化(启动mysql时候报很多错误,初始化)

    ./mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/us ...

  5. Atitit. 包厢记时系统 的说明,教程,维护,故障排查手册v2 pb25.doc

    Atitit. 包厢记时系统 的说明,教程,维护,故障排查手册v2 pb25.doc 1. 服务器方面的维护1 1.1. 默认情况下,已经在系统的启动目录下增加了 个启动项目1 1.2. 后台服务.保 ...

  6. 红茶一杯话Binder (传输机制篇_下)

    红茶一杯话Binder (传输机制篇_下) 侯 亮 1 事务的传递和处理 从IPCThreadState的角度看,它的transact()函数是通过向binder驱动发出BC_TRANSACTION语 ...

  7. Swift 烧脑体操一

    Swift 烧脑体操(一) - Optional 的嵌套   前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融 ...

  8. AutoLayout详解+手把手实战(转载)

    首先说一下这篇博客虽然是标记为原创,但是事实并非本人亲自写出来的,知识点和例子本人花了一天各处查 找和整理最终决定写一个汇总的详解,解去各位朋友到处盲目查找的必要,因为不是转载某一个人的内容,故此不标 ...

  9. linux设备树语法

    设备树语法及绑定 概述 Device Tree是一种用来描述硬件的数据结构,类似板级描述语言,起源于OpenFirmware(OF). 就ARM平台来说,设备树文件存放在arch/arm/boot/d ...

  10. sqlite3 PC安装及使用

    sqlite3使用 1. 安装sqlite3 sudo apt-get install sqlite3 sudo apt-get install libsqlite3-dev 2. sqlite常用命 ...