Advertisement
Guest User

Graf

a guest
Jun 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. // Graf.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. struct list {
  10.     int vertex;
  11.     list* next;
  12. };
  13.  
  14. int Matrix(int n, int m) {
  15.     int A[10][10], x, y, c;
  16.     for (int i=1; i<=n; i++)
  17.         for (int j = 1; j <=n; j++) {
  18.             A[i][j] = 0;
  19.         }
  20.     for (int i = 1; i <=m; i++) {
  21.         cout << "Ввод откуда идет ребро, куда идет, вес ребра " << endl;
  22.         cin >> x >> y >> c;
  23.         cout << endl;
  24.         A[x][y] = c;
  25.         A[y][x] = c;
  26.     }
  27.  
  28.     for (int i = 1; i <= n; i++) {
  29.         cout << endl;
  30.         for (int j = 1; j <= n; j++) {
  31.             cout << " " << A[i][j];
  32.         }
  33.     }
  34.  
  35.     return 0;
  36. }
  37.  
  38. void inlist(int n, int m) {
  39.     int x, y, i;
  40.     list* HEAD[10];
  41.     for (int i = 1; i <= n; i++) {
  42.         HEAD[i] = new(list);
  43.         HEAD[i]->next = 0;
  44.         HEAD[i]->vertex = i;
  45.     }
  46.     for (int i = 1; i <= m; i++) {
  47.         cout << "Введите откуда идет реберо, куда идет " << endl;
  48.         cin >> x >> y;
  49.         cout << endl;
  50.         if (HEAD[x]->next == 0) {
  51.             list* q = new(list);
  52.             q->vertex = y;
  53.             HEAD[x]->next = q;
  54.             q->next = 0;
  55.         }
  56.         else {
  57.             list*p = HEAD[x]->next;
  58.             while (p->next != 0) p = p->next;
  59.             list* q = new(list);
  60.             q->vertex = y;
  61.             q->next = 0;
  62.             p->next = q;
  63.         }
  64.     }
  65.     for (int i = 1; i <= n; i++) {
  66.         cout << "Вершина " << HEAD[i]->vertex << " Ведет в ";
  67.         list *p = new(list);
  68.         p = HEAD[i]->next;
  69.         while (p != 0) {
  70.             cout << p->vertex;
  71.             p = p->next;
  72.         }
  73.         if (HEAD[i]->next == 0) cout << "Пусто ";
  74.         cout << endl;
  75.  
  76.     }
  77. }
  78.  
  79. int main()
  80. {
  81.     setlocale(0, "");
  82.  
  83.     //--------------------INPUT VIA ADJACENCY MATRIX--------------------------------/
  84.     /*
  85.     int n;
  86.     cout << "Введите кол-во вершин в графе ";
  87.     cin >> n;
  88.     cout << endl;
  89.     cout << "Введите кол-во ребер ";
  90.     cin >> m;
  91.     cout << endl;
  92.     Matrix(n,m);
  93.     */
  94.     //------------------------------------------------------------------------------/
  95.    
  96.     //--------------------INPUT VIA ADJACENCY LIST----------------------------------/
  97.     /*
  98.     int n, m;
  99.     cout << "Введите количество вершин в графе ";
  100.     cin >> n;
  101.     cout << endl;
  102.     cout << "Введите количество ребер в графе ";
  103.     cin >> m;
  104.     cout << endl;
  105.    
  106.     inlist(n, m);
  107.     */
  108.     //------------------------------------------------------------------------------/
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement