Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. //Zadanie 1
  8. const int V = 6;
  9. int G[V][V] = { 0,1,0,0,1,0,
  10. 0,0,1,0,0,1,
  11. 0,1,0,0,1,0,
  12. 0,0,1,0,0,1,
  13. 1,0,0,0,0,0,
  14. 1,0,0,1,0,0 };
  15.  
  16. bool visited[V] = { 0 };
  17.  
  18. /*
  19. struct ElementG {
  20. ElementG *next;
  21. int vertex;
  22. };
  23. */
  24.  
  25. std::queue<int> q;
  26.  
  27. void bfs(int i) {
  28. int v = 0;
  29.  
  30. for (int i = 0; i < V; i++)
  31. visited[i] = 0;
  32.  
  33. // ElementG *temp = NULL, *head = NULL, *tail = NULL;
  34.  
  35. /*temp = new ElementG;
  36. temp->vertex = a;
  37. temp->next = NULL;
  38. if (head == NULL)
  39. head = tail = temp;
  40. else {
  41. tail->next = temp;
  42. tail = temp;
  43. }*/
  44.  
  45. q.push(i);
  46. visited[i] = true;
  47.  
  48. cout << i << " --> ";
  49.  
  50. while (!q.empty() /*head != NULL */ ) {
  51.  
  52. v = q.front();
  53. q.pop();
  54.  
  55. /*
  56. v = head->vertex;
  57.  
  58. temp = head;
  59. head = head->next;
  60. delete temp;
  61. */
  62.  
  63. for (int i = 0; i < V; i++) {
  64. if (G[v][i] == 1 && visited[i] == false) {
  65.  
  66. /*
  67. temp = new ElementG;
  68. temp->vertex = i;
  69. temp->next = NULL;
  70. if (head == NULL) {
  71. head = tail = temp;
  72. }
  73. else {
  74. tail->next = temp;
  75. tail = temp;
  76. }
  77. */
  78.  
  79. q.push(i);
  80. visited[i] = true;
  81. std::cout << " " << i << " -->";
  82. }
  83. }
  84. }
  85. std::cout << " END";
  86. }
  87. void graf() {
  88. for (int i = 0; i < V; i++) {
  89. cout << endl;
  90. for (int j = 0; j < V; j++)
  91. cout << G[i][j] << " ";
  92. }
  93. } //wyswietlenie grafu
  94.  
  95. void zadanie1() {
  96. graf();
  97. cout << endl << endl;
  98. bfs(0);
  99. cout << endl << endl;
  100. bfs(5);
  101. }
  102.  
  103. //Zadanie 2
  104. struct ElementS {
  105. ElementS *next;
  106. char value;
  107. };
  108.  
  109. ElementS *head = NULL;
  110.  
  111. void push(char z) {
  112.  
  113. ElementS *nowy = new ElementS;
  114. nowy->value = z;
  115.  
  116. if (head == NULL) {
  117. nowy->next = NULL;
  118. head = nowy;
  119. }
  120. else {
  121. nowy->next = head;
  122. head = nowy;
  123. }
  124. }
  125. char pop() {
  126. ElementS *temp = NULL;
  127.  
  128. char a;
  129. if (head == NULL) {
  130. cout << "stos pusty!\n";
  131. }
  132. else {
  133. a = head->value;
  134. temp = head;
  135. head = head->next;
  136. delete temp;
  137. return a;
  138. }
  139. }
  140. void clear() {
  141. ElementS *temp = NULL;
  142.  
  143. while (head != NULL) {
  144. temp = head;
  145. head = head->next;
  146. delete temp;
  147. }
  148. }
  149. void print() {
  150.  
  151. ElementS *temp = head;
  152. while (temp != NULL) {
  153. cout << temp->value << " ";
  154. temp = temp->next;
  155. }
  156. cout << endl << endl;
  157. } //wydruk stanu stosu
  158.  
  159. void zadanie2() {
  160. push('a'); print(); push('e');
  161. print(); push('i'); print(); push('n'); print(); push('e'); print();
  162. cout << pop(); print(); cout << pop(); print();
  163. push('r'); print(); push('x'); print(); clear(); print();
  164. push('a'); print(); push('e'); print(); push('i'); print(); push('n');
  165. cout << pop(); print(); cout << pop(); print(); cout << pop(); print(); cout << pop(); print();
  166. }
  167.  
  168. int main(int argc, char** argv) {
  169.  
  170. cout << "Zadanie 1" << endl; zadanie1();
  171. cout << endl << endl;
  172. cout << "Zadanie 2" << endl; zadanie2();
  173.  
  174. system("PAUSE>nul");
  175. return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement