Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <list>
- using namespace std;
- class Graph{
- private:
- int n; //number of vertex in the graph
- list<int>* listAd; // Pointer to an list containing adjacency lists
- void DFSUtil(int u, bool visited[]);
- public:
- Graph(int n=10);
- void addEdge(int v, int w); // function to add an edge to graph
- void print(int v);
- };
- Graph::Graph(int n){
- this->n = n;
- listAd = new list<int>[n];
- }
- void Graph::addEdge(int u, int v){
- listAd[u].push_back(v);
- }
- void Graph::print(int v){
- list<int>::iterator it;
- for (it = listAd[v].begin(); it != listAd[v].end(); ++it)
- cout<<*it<<" ";
- }
- int main(){
- Graph g(4);
- g.addEdge(0, 1);
- g.addEdge(0, 2);
- g.addEdge(1, 2);
- g.addEdge(2, 0);
- g.addEdge(2, 1);
- g.addEdge(2, 3);
- g.addEdge(3, 3);
- cout << "Graph"<<endl;
- for(int i=0; i<4; i++){
- g.print(i);
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement