Advertisement
monicahsu

gragh

Dec 12th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. class node
  2. {
  3. public:
  4. int value;
  5. node *next = NULL;
  6. }
  7.  
  8. class image
  9. {
  10. public:
  11. image()
  12. {
  13. for(int i = 0; i < size; i++)
  14. {
  15. gragh[i] = new node;
  16. }
  17. }
  18. node *gragh[size];
  19. void create();
  20. void list();
  21. }
  22.  
  23. void image::create()
  24. {
  25. for(int i = 0; i < size; i++)
  26. {
  27. for(int j = 0; j < size; j++)
  28. {
  29. node* x=new node();
  30. x->value = j;
  31.  
  32. if(fig[i][j] == 1)
  33. {
  34. node* p = gragh[i];
  35. while(p->next != NULL)
  36. {
  37. p = p->next;
  38. }
  39. p->next = x;
  40. }
  41. }
  42. }
  43. }
  44.  
  45. void image::list()
  46. {
  47. for(int i = 0; i < size; i++)
  48. {
  49. cout<<"頂點:"<<i+1<<"=>";
  50. node* p = gragh[i]->next;
  51. while(p != NULL)
  52. {
  53. cout<<"["<< p->value+1 <<"]\t";
  54. p = p->next;
  55. }
  56. cout<<endl;
  57. }
  58. }
  59.  
  60. void visit(int i, int *isvisit, node** head)
  61. {
  62. node* ptr;
  63. isvisit[i] = 1;
  64. cout<< "visit=" << i+1 <<endl;
  65. ptr = head[i]->next;
  66. while(ptr != NULL)
  67. {
  68. if(isvisit[ptr->value] == 0)
  69. visit(ptr->value, isvisit, head);
  70. ptr = ptr->next;
  71. }
  72. }
  73.  
  74. void main()
  75. {
  76. image pl;
  77. int isvisit[size];
  78. for(int i = 0; i < size; i++)
  79. {
  80. isvisit[i] = 0;
  81. }
  82. pl.create();
  83. pl.list();
  84. visit(0, isvisit, pl.gragh);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement