C数据结构-查找(监视哨)

作者在 2006-12-05 23:56:00 发布以下内容

编程完成:使用“监视哨”方法实现顺序查找。下面给出一个程序,试试把它放到你的机上运行一下,然后将你遇到的问题分析分析……(此程序已在Turbo C for Windows运行过)。

#include <stdlib.h>
#include <stdio.h>
#define MAX 5  /*最大数组容量*/

typedef struct
{
 int r[MAX+1];  /*r[0]为工作单元*/
 int length;
}recordlist;

/*输入一组数据*/
int input(recordlist l)
{
 int i;
 l.length=MAX;
 printf("\nPlease input numbers:\n");
 for(i=1;i<=MAX;i++)
 scanf("%d",&l.r);
}

/*使用监视哨顺序查找*/
int search(recordlist l,int k)
{
 int pos=l.length;
 l.r[0]=k;   /*监视哨*/
 while(l.r[pos]!=k)
    pos--;
 return(pos);
}

void main( )/*主函数*/
{
 int i,k;
 recordlist l;
 input(l);
 printf("\nInput the number you want to find:\n");
 scanf("%d",&k);
 i=search(l,k);
 if(i!=0)
    printf("\n%d in the place:%d.\n",k,i);
 else
    printf("\n%d is not exist.\n",k);
 exit(k);
}

C | 阅读 4141 次
文章评论,共0条
游客请输入验证码