SHARE
TWEET

graph_node

cyjyj_0524 Dec 6th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ConsoleApplication5.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7. #define size 10
  8.  
  9. int fig[10][10] = {
  10.     { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
  11.     { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
  12.     { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  13.     { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
  14.     { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
  15.     { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
  16.     { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
  17.     { 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
  18.     { 0, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
  19.     { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
  20. };
  21.  
  22. class node{
  23. public:
  24.     int value;
  25.     node *next = NULL;
  26. };
  27.  
  28. class image{
  29. public:
  30.     image(){ for (int i = 0; i < size; i++){ graph[i] = new node; } }
  31.     node* graph[size];
  32.     void create();
  33.     void list();
  34. };
  35.  
  36. void image::create(){
  37.  
  38.    
  39.     for (int i = 0; i < size; i++){
  40.         for (int j = 0; j < size; j++){
  41.             node* x = new node();
  42.             x->value = j;
  43.  
  44.             if (fig[i][j] == 1){
  45.                 node* p = graph[i];
  46.                 while (p->next != NULL){
  47.                     p = p->next;
  48.                 }
  49.                 p->next = x;
  50.  
  51.             }
  52.                
  53.         }
  54.     }
  55. }
  56.  
  57. void image::list(){
  58.     for (int i = 0; i < size; i++){
  59.         cout << "頂點 " << i + 1 << "=>";
  60.         node* p = graph[i]->next;
  61.         while (p!= NULL){
  62.             cout << "[" << p->value+1 << "]\t";
  63.             p = p->next;
  64.         }
  65.         cout << endl;
  66.     }
  67. }
  68.  
  69. void visit(int i, int*isvisit, node** head){
  70.     node* ptr;
  71.     isvisit[i] = 1;
  72.     cout << "visit= " << i+1 << endl;
  73.     ptr = head[i]->next;
  74.     while (ptr != NULL){
  75.         //cout << "this point:" << i+1 << endl;
  76.         //cout << "check pint:" << ptr->value + 1 << endl;
  77.         if (isvisit[ptr->value] == 0)
  78.             visit(ptr->value, isvisit, head);
  79.         ptr = ptr->next;
  80.     }
  81. }
  82.  
  83.  
  84. void main()
  85. {
  86.     image p1;
  87.     int isvisit[size];
  88.     for (int i = 0; i < size; i++)
  89.     {
  90.         isvisit[i] = 0;
  91.     }
  92.     p1.create();
  93.     p1.list();
  94.     visit(0, isvisit, p1.graph);
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top