fork download
  1. #include "mygraph.h"
  2. #include <cstddef>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. node* build_graph(int n)
  7. {
  8. node* p;
  9. p = new node[n];
  10. for(int i=0;i<n;i++)
  11. {
  12. p[i].next = NULL;
  13. p[i].index = i;
  14. }
  15. cout<<"make graph complete\n";
  16. return p;
  17. }
  18. void graph_connect(node* graph,int a,int b)
  19. {
  20. //connect node a and node b (a,b are index)
  21. node* p;
  22. p = graph[a].next;
  23. while(p!=NULL)
  24. {
  25. p = p->next;
  26. if(p->index==b)
  27. {
  28. return; //already connected
  29. cout<<"already connect\n";
  30. }
  31. }
  32. p = new node;
  33. p->index = b;
  34. p->next = NULL;
  35. cout<<"connect!\n";
  36. }
  37.  
  38.  
  39. void show_graph(node* graph,int n)
  40. {
  41. for(int i=0;i<n;i++)
  42. {
  43. cout<<graph[i].index;
  44. node* p = graph[i].next;
  45. while(p!=NULL)
  46. {
  47. cout<<" -> "<< p->index;
  48. p = p->next;
  49. }
  50. cout<<endl;
  51. }
  52. }
  53.  
  54. bool query_connect(node* graph,int a, int b)
  55. {
  56. node* p = graph[a].next;
  57. while(p!=NULL)
  58. {
  59. if(p->index == b)
  60. return true;
  61. else
  62. p = p->next;
  63. }
  64. return false;
  65. }
  66.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty