一,按位查找
//直接返回顺序表中第 i 个数据即可。
int GetElem(SqList L, int i) {
//需要验证i是否合法。
return L.data[i - 1];
}
时间复杂度: o(1);
二,按值查找
//顺序表的按值查找
int LocateElem(SeqList L, int e) { //寻找表中值为e 的元素
for (int i = 0; i < L.length - 1; i++) {
//从头元素检索
if (L.data[i] == e) { //不可用于结构体顺序表的查找
return i+1; //返回值为e的元素的位次。
}
return 0;
}
}
//对于判断结构体元素是否相等
//定义一个结构体顺序表
typedef struct {
int num;
int people;
}Customer;
bool isCustomerEqual(Customer a, Customer b) {
if (a.num == b.num && a.people == b.people) {
return true;
}
else {
return false;
}
时间复杂度:
最好:循环一次:复杂度 = o(1)
最坏:循环n次:复杂度 = o(n)
平均:平均循环次数 n +1/2: 复杂度= o(n)