/***************************************************/
/* Description:线性表的查找_顺序查找(C语言源代码)
/* Date:2021/9/1
* Author:汝南城
/****************************************************/
#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct {
int key;
char name[10]; /*other information*/
}ElemType;
typedef struct {
ElemType* R;
int length;
}SSTable;
/*①初始化顺序表*/
int Init_Sqe(SSTable* ST)
{
ST->R = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
if (!ST->R)
return 0;
else
{
ST->length = 0;
return 1;
}
}
/*②顺序查找*/
int Search_Sqe(SSTable ST, int key)
{
ST.R[0].key = key; /*哨兵*/
int i;
for (i = ST.length; key != ST.R[i].key; i--);
return i; /*返回0,表示查找失败*/
}
void main()
{
SSTable* S1 = (SSTable*)malloc(sizeof(SSTable));
/*①初始化顺序表*/
int a = 0;
a = Init_Sqe(S1);
//printf("%d",a);
/*往顺序表里存数据*/
ElemType info;
for (info.key = 1; info.key <= 10; info.key++)
{
S1->R[info.key].key = info.key+50;
}
/*打印顺序表中的数据*/
int j;
for (j = 1; j <= 10; j++)
{
printf("%d ", S1->R[j].key);
S1->length++;
}
printf("\n%d", S1->length);
/*②顺序查找*/
int c = 0;
c = Search_Sqe(*S1, 57);
printf("\n%d", c);
}
线性表的查找_顺序查找(C语言源代码)
最新推荐文章于 2022-08-01 23:27:24 发布
该博客展示了如何使用C语言实现线性表的顺序查找。通过`Init_Seq`函数初始化顺序表,然后利用`Search_Seq`函数进行查找操作。博主`汝南城`提供了完整的代码实现,包括顺序表的初始化、数据存储和查找过程,便于读者理解和学习C语言编程中的数据结构应用。
2391

被折叠的 条评论
为什么被折叠?



