LordMirai

grafuri

Apr 27th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <list>
  5. #include <queue>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <math.h>
  9.  
  10. #define N 13
  11. #define q 3
  12.  
  13. using namespace std;
  14.  
  15. int fdisp(int x) {
  16. return (q - x % q) % N;
  17. }
  18.  
  19. bool cautare(list <int> H[], int x) {
  20. int cheie = fdisp(x);
  21. auto rezultat = find(H[cheie].begin(), H[cheie].end(), x);
  22. if (rezultat != H[cheie].end())
  23. return true;
  24. return false;
  25. }
  26.  
  27. void inserare(list <int> H[], int x) {
  28. if (cautare(H, x)) {
  29. cout << "Valoarea exista in tabel.\n";
  30. return;
  31. }
  32. int cheie = fdisp(x);
  33. H[cheie].push_back(x);
  34. }
  35.  
  36. void eliminare(list <int> H[], int x) {
  37. if (!cautare(H, x)) {
  38. cout << "Valoarea nu exista in tabel.\n";
  39. return;
  40. }
  41. int cheie = fdisp(x);
  42. H[cheie].remove(x);
  43. }
  44.  
  45. void afisaretab(list <int> H[]) {
  46. for (int i = 0; i < N; i++) {
  47. cout << "H[" << i << "] = ";
  48. for (int x : H[i]) {
  49. cout << x << " ";
  50. }
  51. cout << endl;
  52. }
  53. cout << "\n\n";
  54. }
  55.  
  56. void citireMat(int A[10][10], int& n, int& m) {
  57. ifstream inp("D:\\graf.txt");
  58. int x, y;
  59. inp >> n >> m;
  60. for (int i = 1; i <= m; i++) {
  61. inp >> x >> y;
  62. A[x][y] = 1;
  63. A[y][x] = 1;
  64. }
  65. }
  66.  
  67. void afisMat(int A[10][10], int& n) {
  68. for (int i = 1; i <= n; i++) {
  69. for (int j = 1; j <= n; j++) {
  70. cout << A[i][j] << " ";
  71. }
  72. cout << endl;
  73. }
  74. }
  75.  
  76. void citListaAd(list <int> LA[10], int& n, int& m) {
  77. ifstream inp("D:\\graf.txt");
  78. int x, y;
  79. inp >> n >> m;
  80. for (int i = 1; i <= m; i++) {
  81. inp >> x >> y;
  82. LA[x].push_back(y);
  83. LA[y].push_back(x);
  84. for (int i = 1; i < n; i++) {
  85. LA[i].sort();
  86. }
  87. }
  88. }
  89.  
  90. void afisListaAd(list <int> lista) {
  91. for (list <int> ::iterator it = lista.begin(); it != lista.end(); it++) {
  92. cout << *it << " ";
  93. }
  94. cout << endl;
  95. }
  96.  
  97. void grafuriMatrice() {
  98. // matrice adiacenta:
  99. ifstream inp("D:\\graf.txt");
  100. int A[10][10];
  101.  
  102. for (int i = 1; i <= 10; i++) {
  103. for (int j = 1; j <= 10; j++)
  104. A[i][j] = 0;
  105. }
  106.  
  107. int x, y;
  108. x = y = 10;
  109. citireMat(A, x, y);
  110. afisMat(A, y);
  111. }
  112.  
  113. void grafuriListe() {
  114. int n = 5, m = 5;
  115. list <int> listaAd[10];
  116. citListaAd(listaAd, n,m);
  117. for (int i = 1; i <= n; i++) {
  118. cout << i << ": ";
  119. afisListaAd(listaAd[i]);
  120. }
  121. }
  122.  
  123. int main() {
  124. grafuriListe();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment