Making an adjacency list in C++ for a directed graph

后端 未结 5 1987
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 01:19

Hello all :) Today I am refining my skills on graph theory and data structures. I decided to do a small project in C++ because it\'s been a while since I\'ve worked in C++

5条回答
  •  迷失自我
    2021-01-02 01:38

    This may not be very general approach but thats how I handle adjacency list in most of the cases. C++ has STL library which supports a data structure for linked list named as list.

    Say you have N nodes in the graph, create a linked list for every node.

    list graph[N];
    

    Now graph[i] represent the neighbours of node i. For every edge i to j, do

    graph[i].push_back(j);
    

    The best comfort is no handling of pointers so as segmentation fault errors.

    For more reference http://www.cplusplus.com/reference/list/list/

提交回复
热议问题