Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("date.in");
  7. ofstream fout("data.out");
  8. template<typename T>
  9. class Queue
  10. {
  11. typedef struct nod
  12. {
  13. int info;
  14. nod *next;
  15. }queue;
  16. queue *first, *last;
  17. public:
  18. Queue();
  19. ~Queue();
  20. void push(T);
  21. void pop();
  22. T peek() const;
  23. T peek_possition(unsigned)const;
  24. void show()const;
  25. bool isEmpty()const;
  26. int size()const;
  27. };
  28.  
  29. template<typename T>Queue<T>::Queue()
  30. {
  31. first = last = NULL;
  32. }
  33.  
  34. template<typename T>Queue<T>::~Queue()
  35. {
  36. queue *elem;
  37. if(first)
  38. while (last)
  39. {
  40. elem = last;
  41. last = last->next;
  42. delete elem;
  43. }
  44. }
  45.  
  46. template<typename T>void Queue<T>::push(T val)
  47. {
  48. if (first == NULL)
  49. {
  50. first = new queue;
  51. first->info = val;
  52. first->next = NULL;
  53. last = first;
  54. }
  55. else
  56. {
  57. queue *p = new queue;
  58. p->info = val;
  59. p->next = NULL;
  60. first->next = p;
  61. first = p;
  62. }
  63. }
  64.  
  65. template<typename T>void Queue<T>::show()const
  66. {
  67. queue *p = last;
  68. while (p)
  69. {
  70. fout << p->info << " ";
  71. p = p->next;
  72. }
  73. }
  74.  
  75. template<typename T>void Queue<T>::pop()
  76. {
  77. queue *p = last;
  78. last = last->next;
  79. delete p;
  80. }
  81.  
  82. template<typename T>T Queue<T>::peek()const
  83. {
  84. return last->info;
  85. }
  86.  
  87. template<typename T>T Queue<T>::peek_possition(unsigned possition)const
  88. {
  89. int count = 1;
  90. queue *p = last;
  91. while (p && count != possition)
  92. {
  93. p = p->next;
  94. count++;
  95. }
  96. if (p)
  97. return p->info;
  98. else
  99. return -1;
  100. }
  101.  
  102. template<typename T>bool Queue<T>::isEmpty()const
  103. {
  104. if (!last)
  105. return true;
  106. else
  107. return false;
  108. }
  109.  
  110. template<typename T>int Queue<T>::size()const
  111. {
  112. int count = 1;
  113. queue *p = last;
  114. if (p)
  115. while (p)
  116. {
  117. p = p->next;
  118. count++;
  119. }
  120. else
  121. count = 0;
  122.  
  123. return count;
  124. }
  125.  
  126. #define NMAX 100
  127. class Graph
  128. {
  129. int edge, nods,viz[NMAX];
  130. Queue<int> graph[NMAX],queue;
  131. public:
  132. Graph();
  133. ~Graph();
  134. void adiacent_list();
  135. void breadth();
  136. };
  137.  
  138. Graph::Graph()
  139. {
  140. int x, y;
  141. fin >> nods;
  142. fin >> edge;
  143. for (unsigned int i = 1;i <= edge;i++)
  144. {
  145. fin >> x >> y;
  146. graph[x].push(y);
  147. graph[y].push(x);
  148. }
  149. }
  150.  
  151. void Graph::breadth()
  152. {
  153. int st, dr, i, x[50], j, p[50];
  154. st = dr = 1;
  155. for (int i = 1;i <= 49;i++)
  156. p[i] = 0;
  157. p[1] = 1; // daca vrei sa incepi cu alt nod ca start trebuie sa pui p[ nodul tau] = 1
  158. queue.push(1); //si il bagi in coada queue.push(nodul tau)
  159. while (st <= dr)
  160. {
  161. for (i = 1;i <= graph[queue.peek_possition(st)].size();i++)
  162. {
  163. j = graph[queue.peek_possition(st)].peek_possition(i);
  164. if (!p[j])
  165. {
  166. dr++;
  167. queue.push(j);
  168. p[j] = 1;
  169. }
  170. }
  171. st++;
  172. }
  173.  
  174. adiacent_list();
  175.  
  176. queue.show();
  177. }
  178.  
  179. void Graph::adiacent_list()
  180. {
  181. for (unsigned int i = 1;i <= nods;i++)
  182. {
  183. fout << i << " : ";
  184. while (!graph[i].isEmpty())
  185. {
  186. fout << graph[i].peek() << " ";
  187. graph[i].pop();
  188. }
  189. fout << endl;
  190. }
  191. }
  192.  
  193. Graph::~Graph()
  194. {
  195. }
  196.  
  197. void main()
  198. {
  199. Graph g;
  200. g.breadth();
  201.  
  202. system("pause");
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement