node[i] と node[i+1] が neighbour[i] に存在する場合、node の 'i' 位置を格納し、node の '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");
}
}