SHARE
TWEET

graph-visit(easy input ver)

gha890826 Dec 6th, 2019 82 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. #include <string>
  7. #include <cctype>
  8. using namespace std;
  9. #define map_size 10
  10.  
  11. class node
  12. {
  13. public:
  14.     node(int n){ next = NULL; data = n; }
  15.     int data;
  16.     node *next;
  17. };
  18.  
  19. class list
  20. {
  21. public:
  22.     void push(int);
  23.     list(){ head = NULL; }
  24.     void print();
  25.     node *head;
  26. private:
  27. };
  28.  
  29. void list::push(int push_in)
  30. {
  31.     //cout << "\ncall push\n";
  32.     if (head == NULL)
  33.     {
  34.         head = new node(push_in);
  35.         return;
  36.     }
  37.     node* pos = list::head;
  38.     while (pos->next != NULL)
  39.     {
  40.         pos = pos->next;
  41.     }
  42.     pos->next = new node(push_in);
  43.     //cout << "push end\n";
  44. }
  45.  
  46. void list::print()
  47. {
  48.     node *pos = head;
  49.     while (pos != NULL)
  50.     {
  51.         cout << "[" << pos->data + 1 << "]";
  52.         pos = pos->next;
  53.     }
  54. }
  55.  
  56. class graph
  57. {
  58. public:
  59.     graph();
  60.     list maplist[map_size];
  61.     bool isvisit[map_size];
  62.     void visit(int, bool *, list*);
  63.     void convert_list();
  64.     void print_list();
  65. };
  66.  
  67. graph::graph()
  68. {
  69.     cout << "\nhave to caret map :\n";
  70.     for (int i = 0; i < map_size; i++)
  71.     {
  72.         cout << "V" << i + 1 << "can go :";
  73.         string in;
  74.         getline(cin, in);
  75.         for (int j = 0; j < in.length(); j++)
  76.         {
  77.             if (isdigit(in[j]))
  78.             {
  79.                 if (in[j + 1] == '0')
  80.                 {
  81.                     maplist[i].push(9);
  82.                     j++;
  83.                 }
  84.                 else
  85.                     maplist[i].push(int(in[j] - '0')-1);
  86.             }
  87.         }
  88.     }
  89.     for (int i = 0; i < map_size; i++)
  90.         isvisit[i] = 0;
  91.     cout << "creat success!\n\n";
  92. }
  93.  
  94. void graph::print_list()
  95. {
  96.     for (int i = 0; i < map_size; i++)
  97.     {
  98.         cout << "V" << i + 1 << " :";
  99.         maplist[i].print();
  100.         cout << endl;
  101.     }
  102. }
  103.  
  104. void graph::visit(int i, bool *isvisit, list* head)
  105. {
  106.     node *ptr;
  107.     isvisit[i] = 1;
  108.     cout << "visit= " << i + 1 << endl;
  109.     ptr = head[i].head;
  110.     while (ptr != NULL)
  111.     {
  112.         if (isvisit[ptr->data] == 0)
  113.         {
  114.             visit(ptr->data, isvisit, head);
  115.         }
  116.         ptr = ptr->next;
  117.     }
  118. }
  119.  
  120. int main()
  121. {
  122.     graph gr1;
  123.  
  124.     gr1.print_list();
  125.  
  126.     gr1.visit(0, gr1.isvisit, gr1.maplist);
  127.     return 0;
  128. }
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