Advertisement
Toxotsist

Самая простая реализация графа

Nov 24th, 2021
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct node {
  6.     int i = NULL;
  7.     vector <int> edges;
  8.     node* nextVertex;
  9.     node(int n) {
  10.         i = n;
  11.         nextVertex = NULL;
  12.     }
  13. };
  14.  
  15. typedef node Node;
  16.  
  17. class Graph {
  18.     Node* head;
  19.     int N;
  20. public:
  21.     Graph(int t){
  22.         this->N = t;
  23.     }
  24.     void addEdge(int, int, bool);
  25.     void addVertex(int);
  26.     node* existVertex(int);
  27.     void print();
  28. };
  29.  
  30. node* Graph::existVertex(int i) {
  31.     node* cur = this->head;
  32.     while (cur != NULL) {
  33.         if (cur->i == i) {
  34.             return cur;
  35.             break;
  36.         }
  37.         cur = cur->nextVertex;
  38.     }
  39.     return 0;
  40. }
  41.  
  42. void Graph::addEdge(int i, int j, bool status) {
  43.    
  44.     if (this->head == NULL) {
  45.         this->head = new node(i);
  46.         this->head->edges.push_back(j);
  47.         node* cur = this->head;
  48.         if (status == true) {
  49.             cur->nextVertex = new node(j);
  50.             cur->nextVertex->edges.push_back(i);
  51.         }
  52.     }
  53.     else if (existVertex(i) != NULL) {
  54.         node* cur = existVertex(i);
  55.         cur->edges.push_back(j);
  56.         if (existVertex(j) == NULL)
  57.         cur->nextVertex = new node(j);
  58.         if (status == true) {
  59.             cur->nextVertex->edges.push_back(i);
  60.         }
  61.     }
  62.     else {
  63.         node* cur = this->head;
  64.         while (cur->nextVertex != NULL) cur = cur->nextVertex;
  65.         cur->nextVertex = new node(i); cur->edges.push_back(j);
  66.         if (existVertex(j) == NULL)
  67.         cur->nextVertex = new node(j);
  68.         if (status == true) {
  69.             cur->nextVertex->edges.push_back(i);
  70.         }
  71.     }
  72. }
  73.  
  74. void Graph::addVertex(int i) {
  75.     if (existVertex(i) == NULL) {
  76.         node* cur = this->head;
  77.         while (cur->nextVertex != NULL) cur = cur->nextVertex;
  78.         cur->nextVertex = new node(i);
  79.     }
  80. }
  81.  
  82. void Graph::print() {
  83.     node* cur = this->head;
  84.     while (cur != NULL) {
  85.         cout << cur->i << " -> ";
  86.         if (cur->edges.size() == 1) {
  87.             cout << *cur->edges.begin();
  88.         }
  89.         else {
  90.             auto it = cur->edges.begin();
  91.             auto end = cur->edges.end();
  92.             while (it != end) {
  93.                 cout << *it << " ";
  94.                 it++;
  95.             }
  96.         }
  97.         cur = cur->nextVertex;
  98.         printf("\n");
  99.     }
  100. }
  101.  
  102. int main() {
  103.     Graph G(10);
  104.     G.addEdge(1, 7, true);
  105.     G.addEdge(7, 5, true);
  106.     G.addVertex(4);
  107.     G.print();
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement