1.顺序表的查找运算

 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define ElemType int #define MAXSIZE 100 /*此处的宏定义常量表示线性表可能达到的最大长度*/ typedef struct { ElemType elem[MAXSIZE]; /*线性表占用的数组空间*/ int last; /*记录线性表中最后一个元素在数组elem[ ]中的位置(下标值),空表置为-1*/ }SeqList; /*查找函数实现*/ int Locate(SeqList L, ElemType e) { int i=0; /*i为扫描计数器,初值为0,即从第一个元素开始比较*/ while ((i<=L.last)&&(L.elem[i]!=e)) i++; /*顺序扫描表,直到找到值为key的元素, 或扫描到表尾而没找到*/ if (i<=L.last)return(i+1); /*若找到值为e的元素,则返回其序号*/ else return(-1); /*若没找到,则返回空序号*/ } void main() { SeqList l; int p,q,r; int i; printf("Please input the length of SeqList:"); scanf("%d",&r); l.last = r-1; printf("Please input the each number of SeqList:/n"); for(i=0; i<=l.last; i++) { scanf("%d",&l.elem[i]); } printf("please input the number which you want to find:/n"); scanf("%d",&q); p=Locate(l,q); if(p == -1) printf("there is no number in the SeqList!/n"); else printf("The number which you want to find is at the place of :%d/n",p); }

上一篇:linux设备驱动中的并发控制【转】


下一篇:嵌入式,ARM指令基础