/***************************************************/
/* 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);
}
03-02
366
08-01
2932