线性表的查找_顺序查找(C语言源代码)

/***************************************************/
/*    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);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值