Advertisement
Guest User

kamrul

a guest
Dec 6th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. using namespace std;
  2.  
  3. class Node{
  4. public:
  5. int destination;
  6. int weight;
  7. Node *Next ;
  8. };
  9.  
  10. class graphsion
  11. {
  12. public:
  13. Node *head=NULL;
  14. };
  15.  
  16. void AddEdge(graphsion arr[],int src,int dest,int w)
  17. {
  18. Node *newnode = new Node();
  19. newnode->destination = dest;
  20. newnode->weight= w;
  21. newnode->Next = NULL;
  22.  
  23. if(arr[src].head == NULL)
  24. arr[src].head = newnode;
  25.  
  26. else{
  27. Node *temp =arr[src].head;
  28. temp =arr[w].head;
  29. while(temp->Next != NULL)
  30. temp = temp->Next;
  31. temp->Next = newnode;
  32. }
  33.  
  34. }
  35.  
  36. void display(graphsion listt[],int v)
  37. {
  38. int i;
  39. for(i=0;i<v;i++)
  40. {
  41. cout<<"\n adjacency list of vertex-"<<i<<"->";
  42.  
  43. while(listt[i].head != NULL)
  44. {
  45. cout<<" "<<listt[i].head->destination<<"->"<<listt[i].head->weight;
  46. listt[i].head = listt[i].head->Next;
  47. }
  48. }
  49.  
  50. }
  51.  
  52. int main()
  53. {
  54. freopen("input.txt","r",stdin);
  55. int i,v;
  56. cin>>v;
  57. graphsion a[v];
  58. for( i=0;i<=v;i++)
  59. a[i].head=NULL;
  60.  
  61. int m,n,w;
  62. cin>>m>>n>>w;
  63.  
  64. while(scanf("%d%d",&m,&n,&w) == 3)
  65. {
  66. AddEdge(a,m,n,w);
  67. AddEdge(a,n,m,w);
  68.  
  69. }
  70. display(a,v);
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement