如果 node[i] 和 node[i+1] 存在于邻居[i] 中,则存储节点的“i”位置并打印节点的“i”值。
这也可以通过反转节点数组(仅)来完成,并且对邻居 [i] 进行相同类型的检查和打印。
这段代码是我写的,效果很好,有没有其他有效的方法来执行这个。
#include <stdio.h>
int main()
{
int node[5] = {44,5,4,6,40};
int neighbor[4]= {40,3,4,6};
int i=0,j=0,k=0;
int found_indices[5]; // array used to store indices of found entries..
int count = 0; //n entries found;
int fwd_count=0;
int postn;
int find;
// to find in forward direction
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
if (node[i]==neighbor[j]) {
postn=node[i];
for (k=0; k<4; k++) {
if (node[i+1]==neighbor[k]) {
found_indices[fwd_count ++] = postn; // storing the index of found entry
}
}
}
}
}
if (fwd_count!=0) {
for (i=0; i<fwd_count; i++)
printf("forward relay for ==%d\n", found_indices[i]);
} else{
printf("Relay not found in forward\n");
}
// to find in backward direction
for (i=4; i>0; i--) {
for (j=0; j<4; j++) {
if (node[i]==neighbor[j]) {
postn=node[i];
for (k=0; k<4; k++) {
if (node[i-1]==neighbor[k]) {
found_indices[count ++] = postn; // storing the index of found entry
}
}
}
}
}
if (count!=0) {
for (i=0; i<count; i++)
printf("backward relayy for ==%d\n", found_indices[i]);
}else{
printf("Relay not found in backward \n");
}
}