SHARE
TWEET

graph node

wendy890711 Dec 6th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. #define size 10
  5.  
  6.  
  7. int pick[size][size] = {
  8.     { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
  9.     { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
  10.     { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  11.     { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
  12.     { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
  13.     { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
  14.     { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
  15.     { 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
  16.     { 0, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
  17.     { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, };
  18.  
  19.  
  20. class node{
  21. public:
  22.     int data;
  23.     node*next=NULL ;
  24. };
  25.  
  26. class list
  27. {
  28. public:
  29.     void push(int);
  30.     list(){ head = NULL; }
  31.     void print();
  32.     node *head;
  33. private:
  34. };
  35.  
  36. class graph
  37. {
  38. public:
  39.     graph(){ for (int i = 0; i < size; i++){ ima[i] = new node; } }
  40.     node*ima[size];
  41.     void creat();
  42.     void list();
  43. };
  44.  
  45. void graph::creat(){
  46.     for (int i = 0; i < size; i++)
  47.     {
  48.         for (int j = 0; j < size; j++)
  49.         {
  50.             node*x = new node();
  51.             x->data = j;
  52.  
  53.             if (pick[i][j] == 1)
  54.             {
  55.                 node*p = ima[i];
  56.                 while (p->next != NULL)
  57.                     p = p->next;
  58.                 p->next = x;
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. void graph::list(){
  65.     for (int i = 0; i < size; i++)
  66.     {
  67.         node*p = ima[i];
  68.         cout << "頂點" << i + 1 << ":";
  69.         while (p->next != 0)
  70.         {
  71.             p = p->next;
  72.             cout << "[" << p->data+1 << "]";
  73.         }
  74.         printf("\n");
  75.     }
  76. }
  77.  
  78. void visit(int i, int*isvisit, node**head)
  79. {
  80.     node*ptr;
  81.     isvisit[i] = 1;
  82.     cout << "visit:" << i + 1 << endl;
  83.     ptr = head[i]->next;
  84.     while (ptr != NULL)
  85.     {
  86.         if (isvisit[ptr->data] == 0)
  87.             visit(ptr->data, isvisit, head);
  88.         ptr = ptr->next;
  89.     }
  90. }
  91.  
  92. int main()
  93. {
  94.     graph gr1;
  95.     gr1.creat();
  96.     gr1.list();
  97.     int isvisit[size];
  98.     for (int i = 0; i < size; i++)
  99.         isvisit[i] = 0;
  100.     visit(0, isvisit, gr1.ima);
  101. }
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