-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicDataStructure23.cpp
More file actions
88 lines (72 loc) · 1.33 KB
/
DynamicDataStructure23.cpp
File metadata and controls
88 lines (72 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
using namespace std;
struct queue {
int inf;
queue* next;
};
void push(queue *&h, queue *&t, int x) {
queue *r = new queue;
r->inf = x;
r->next = NULL;
if (!h && !t) {
h = t = r;
}
else {
t->next = r;
t = r;
}
}
int pop(queue *&h, queue *&t) {
int i = h->inf;
queue *r = h;
h = h->next;
if (!h) {
t = NULL;
}
delete r;
return i;
}
int main() {
cout << "Enter N = ";
int N; cin >> N;
int** Gr = new int* [N + 1];
for (int i = 0; i < N + 1; i++) {
Gr[i] = new int[N];
}
cout << "Fill the matrix : \n";
for (int i = 0; i < N + 1; i++, cout << '\n') {
for (int j = 0; j < N + 1; j++) {
cout << "Gr[" << i << "][" << j << "] = ";
cin >> Gr[i][j];
}
}
cout << "Enter x = ";
int x; cin >> x;
queue *h = NULL;
queue *t = NULL;
int* A = new int[N + 1];
for (int i = 0; i < N + 1; i++) {
A[i] = 0;
}
A[x] = 1;
push(h, t, x);
cout << x << ' ';
while (h) {
x = pop(h, t);
for (int i = 0; i < N + 1; i++) {
if (A[i] == 0 && Gr[x][i] == 1) {
int y = i;
A[y] = 1;
push(h, t, y);
cout << y << ' ';
}
}
}
/*for (int i = 0; i < N + 1; i++, cout << '\n')
for (int j = 0; j < N + 1; j++) {
cout << Gr[i][j] << " ";
}*/
system("pause");
return 0;
}