kx-000013-顺序表-定位元素下标,locate()-其他
kx-000013-顺序表-定位元素下标,locate()
- 顺序表结构体定义。具体的结构体定义请查看头文件:https://www.cnblogs.com/kxwslmsps/p/16937235.html
typedef status int; //< 定义函数结果状态 typedef int etype; //< 元素数据类型 #define CAPACITY 10 //< 定义初始容量 typedef struct tag_seqList { etype* pbase; //< 表基址 int capacity; //< 表容量 int size; //< 表长度 }mySList;
typedef status int; //< 定义函数结果状态
typedef int etype; //< 元素数据类型
#define CAPACITY 10 //< 定义初始容量
typedef struct tag_seqList
{
etype* pbase; //< 表基址
int capacity; //< 表容量
int size; //< 表长度
}mySList;
- 定位元素key的下标
/**
* @brief 功能:定位元素值key的下标 \n
* @param[in] plist:表结构指针
* @param[in] key:待查找关键字
* @return 返回是否定位成功的结果状态标志
* @retval – ERROR(-1):顺序表不存在,定位失败
* @retval – 非负整数值:key元素所在表中的下标,取值为[0,size-1]
*/
status sList_locate(const mySList* plist, etype key)
{
if (plist == NULL || plist->pbase == NULL)
{
return ERROR;
}
int pos = -1;
for (int i = 0; i < plist->size; ++i)
{
if (key == plist->pbase[i])
{
pos = i;
break;
}
}
return pos;
#if 0 // while循环实现
int pos = plist->size – 1;
while (pos > -1 && plist->pbase[pos] != key)
{
–pos;
}
return pos;
#endif // 0
}// 返回值为key所在下标,可能范围为[-1,size-1],-1表示找不到
/**
* @brief 功能:定位元素值key的下标 \n
* @param[in] plist:表结构指针
* @param[in] key:待查找关键字
* @return 返回是否定位成功的结果状态标志
* @retval - ERROR(-1):顺序表不存在,定位失败
* @retval - 非负整数值:key元素所在表中的下标,取值为[0,size-1]
*/
status sList_locate(const mySList* plist, etype key)
{
if (plist == NULL || plist->pbase == NULL)
{
return ERROR;
}
int pos = -1;
for (int i = 0; i < plist->size; ++i)
{
if (key == plist->pbase[i])
{
pos = i;
break;
}
}
return pos;
#if 0 // while循环实现
int pos = plist->size - 1;
while (pos > -1 && plist->pbase[pos] != key)
{
--pos;
}
return pos;
#endif // 0
}// 返回值为key所在下标,可能范围为[-1,size-1],-1表示找不到
- 顺序表结构体定义。具体的结构体定义请查看头文件:https://www.cnblogs.com/kxwslmsps/p/16937235.html
typedef status int; //< 定义函数结果状态 typedef int etype; //< 元素数据类型 #define CAPACITY 10 //< 定义初始容量 typedef struct tag_seqList { etype* pbase; //< 表基址 int capacity; //< 表容量 int size; //< 表长度 }mySList;
typedef status int; //< 定义函数结果状态
typedef int etype; //< 元素数据类型
#define CAPACITY 10 //< 定义初始容量
typedef struct tag_seqList
{
etype* pbase; //< 表基址
int capacity; //< 表容量
int size; //< 表长度
}mySList;
- 定位元素key的下标
/**
* @brief 功能:定位元素值key的下标 \n
* @param[in] plist:表结构指针
* @param[in] key:待查找关键字
* @return 返回是否定位成功的结果状态标志
* @retval – ERROR(-1):顺序表不存在,定位失败
* @retval – 非负整数值:key元素所在表中的下标,取值为[0,size-1]
*/
status sList_locate(const mySList* plist, etype key)
{
if (plist == NULL || plist->pbase == NULL)
{
return ERROR;
}
int pos = -1;
for (int i = 0; i < plist->size; ++i)
{
if (key == plist->pbase[i])
{
pos = i;
break;
}
}
return pos;
#if 0 // while循环实现
int pos = plist->size – 1;
while (pos > -1 && plist->pbase[pos] != key)
{
–pos;
}
return pos;
#endif // 0
}// 返回值为key所在下标,可能范围为[-1,size-1],-1表示找不到
/**
* @brief 功能:定位元素值key的下标 \n
* @param[in] plist:表结构指针
* @param[in] key:待查找关键字
* @return 返回是否定位成功的结果状态标志
* @retval - ERROR(-1):顺序表不存在,定位失败
* @retval - 非负整数值:key元素所在表中的下标,取值为[0,size-1]
*/
status sList_locate(const mySList* plist, etype key)
{
if (plist == NULL || plist->pbase == NULL)
{
return ERROR;
}
int pos = -1;
for (int i = 0; i < plist->size; ++i)
{
if (key == plist->pbase[i])
{
pos = i;
break;
}
}
return pos;
#if 0 // while循环实现
int pos = plist->size - 1;
while (pos > -1 && plist->pbase[pos] != key)
{
--pos;
}
return pos;
#endif // 0
}// 返回值为key所在下标,可能范围为[-1,size-1],-1表示找不到