Implementation of Graph in C++ using list 

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <vector> #include<list> using namespace std; class Graph { int V; list<pair<int,int>> *l; int weight; public: Graph(int V){ this->V = V; l = new list<pair<int,int>>[V]; } void addEdge(int x , int y , int weight){ l[x].push_back(make_pair(y , weight)); l[y].push_back(make_pair(x , weight)); } void print(){ for(int i=0 ; i<V ; i++){ for(auto it = l[i].begin() ; it!= l[i].end(); it++){ cout<<i<<"->"<<it->first<<"("<<"Weight: "<<it->second<<")"<<endl; } } } }; int main() { Graph g(4); g.addEdge(0,1 , 10); g.addEdge(0,2, 20); g.addEdge(1,2, 21); g.print(); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines