1.顺序表的查找运算

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <malloc.h>
 4 
 5 #define OK   1
 6 #define ERROR  0
 7 #define TRUE 1
 8 #define FALSE 0
 9 
10 #define ElemType int
11 #define    MAXSIZE  100          /*此处的宏定义常量表示线性表可能达到的最大长度*/
12 typedef  struct
13 
14     ElemType  elem[MAXSIZE];  /*线性表占用的数组空间*/
15     int       last;           /*记录线性表中最后一个元素在数组elem[ ]中的位置(下标值),空表置为-1*/
16 }SeqList;
17 
18 /*查找函数实现*/
19 int  Locate(SeqList L, ElemType e)
20 {    
21     int i=0;                                    /*i为扫描计数器,初值为0,即从第一个元素开始比较*/
22     while ((i<=L.last)&&(L.elem[i]!=e))    i++;     /*顺序扫描表,直到找到值为key的元素, 或扫描到表尾而没找到*/
23     if  (i<=L.last)return(i+1);                 /*若找到值为e的元素,则返回其序号*/
24     else  return(-1);                           /*若没找到,则返回空序号*/
25 }
26 
27 void main()
28 {
29     SeqList l;
30     int p,q,r;
31     int i;
32     printf("Please input the length of SeqList:");
33     scanf("%d",&r);
34     l.last = r-1;
35     printf("Please input the each number of SeqList:\n");
36     for(i=0; i<=l.last; i++)
37     {
38         scanf("%d",&l.elem[i]);
39     }
40     printf("please input the number which you want to find:\n");
41     scanf("%d",&q);
42     p=Locate(l,q);
43     if(p == -1) printf("there is no number in the SeqList!\n");
44     else printf("The number which you want to find is at the place of :%d\n",p);
45 }
46 
posted @ 2009-07-15 00:01  清山博客  阅读(2226)  评论(0)    收藏  举报