/* 堆中的路径 */
#include<stdio.h>
#include<stdlib.h>
#define MAXN 1001
#define MINH -1
int main()
{
int heap[MAXN] = {0};
int size=0;
int Data=0;
int j, i;
int arr[]={10,26,46,23,24};
heap[0]=MINH;
for(int k=0;k<5;k++){
if (*(heap + size) != MAXN) {
for (i = ++size; arr[k]< *(heap + i / 2); i /= 2)
*(heap + i) = *(heap + i / 2);
*(heap + i) = arr[k];
}
}
for(int m=0;m<6;m++){
printf("%d\n", heap[m]);
}
printf("please enter Data:\n");
scanf_s("%d", &Data);
for (j = 1; j<MAXN; j++) {
if (heap[j] == Data) break;
else continue;
}
if (j == MAXN) printf("ERROR!\n");
else {
while (j>1) {
printf("%d->", heap[j]);
j /= 2;
}
printf("%d", heap[1]);
}
system("pause");
return 0;
}
输出示例
-1
10
23
46
26
24
please enter Data:
26
26->23->10请按任意键继续. . .