散列表开放定址法的初始化、插入、打印等函数(c)

/* 
 *散列表开放定址法的一些操作。
 */

#include <stdio.h>
#include <stdlib.h>

typedef unsigned int Index;
typedef Index Position;

struct HashTb1;
typedef struct HashTb1 *HashTable;

#define MinTableSize 10
enum KindOfEntry {Legitimate, Empty, Delwted};

struct HashEntry
{
	int Element;
	enum KindOfEntry Info;
};

typedef struct HashEntry Cell;

struct HashTb1
{
	int TableSize;
	Cell *TheCells;
};

static int NextPrime( int N )
{

	if( N % 2 == 0 )
		N++;
	return N;
}

static Index Hash(int key, int TableSize)
{
	return key % TableSize;
}

HashTable InitializeTable(int TableSize)
{
	HashTable H;
	int i;

	if (TableSize < MinTableSize)
	{
		printf("Table size is too small.\n");
		return NULL;
	}
	/* Allocate table */
	H = (HashTable)malloc(sizeof(struct HashTb1));
	if (H == NULL)
	{
		printf("out of space!!!\n");
	}

	H->TableSize = NextPrime(TableSize);

	/* Allocate arrary of cells */
	H->TheCells = (Cell *)malloc(sizeof(Cell) * H->TableSize);
	if (H->TheCells == NULL)
	{
		printf("out of space!!!");
	}

	for (i = 0; i < H->TableSize; i++)
		H->TheCells[i].Info = Empty;

	return H;
}

Position Find(int Key, HashTable H)
{
	Position CurrentPos;
	int CollisionNum;

	CollisionNum = 0;
	CurrentPos = Hash(Key, H->TableSize);
	/* probably need strcmp!! */
	while (H->TheCells[CurrentPos].Info != Empty && H->TheCells[CurrentPos].Element != Key)
	{
		CurrentPos += 2 * ++CollisionNum - 1;
		if (CurrentPos >= H->TableSize)
			CurrentPos -= H->TableSize;
	}
	return CurrentPos;
}

void Insert(int Key, HashTable H)
{
	Position Pos;

	Pos = Find(Key, H);
	if (H->TheCells[Pos].Info != Legitimate )
	{
		H->TheCells[Pos].Info = Legitimate;
		H->TheCells[Pos].Element = Key;
	}
}

void PrintHash(HashTable H)
{
	int i;
	for (i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info == Legitimate)
		{
			printf("%d, ", H->TheCells[i].Element);
		}
		
	}
}

int main(void)
{
	int num = 19;
	HashTable Hash = InitializeTable(19);
	Insert(5, Hash);
	Insert(6, Hash);
	Insert(7, Hash);
	PrintHash(Hash);
	return 0;
}

代码 ADC BEEP TIM2 TIM3 TIM4 EPPROM 直接寄存器操作 /**************************************************************************************** *开发环境:IAR for stm8 v6.5.3 *硬件平台:STM8S005K6T6 REX-C900温控 *功能说明:本例程,利用定时_TIM2中断定时。 *作 者:刘拥军 ****************************************************************************************/ #include<iostm8s005k6.h> #include<stdbool.h> unsigned char BCD_led[9]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00}; //显示缓存区。8_数码段。+1_LED状态输出指示段。 unsigned char lab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x90}; //0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F unsigned char *p=BCD_led,smm=0,s1=0; bool lii=0; void TIM3_Config(void) { CLK_PCKENR1 |= 0x40; //打开外设时钟源TIM2. asm("rim"); while(!(CLK_PCKENR1 & 0x01)); //这些具体数据,一定要看数据手册! TIM3_CR1 = 0x00; TIM3_PSCR = 0x08; //16/2^4 TIM3_ARRH = 0x00; TIM3_ARRL = 0x08; //重装值 TIM3_CNTRH = 0x00; TIM3_CNTRL = 0x00; //计数器 TIM3_IER = 0x01; //开更新中断 TIM3_CR1 = 0x01; //定时器使能 } void adc_int() //初始化A/D模块 { ADC_CR2 = 0x00; // A/D结果数据左对齐 ADC_CR1 = 0x00; // ADC时钟=主时钟/2=1MHZ,ADC转换模式=单次,禁止ADC转换 ADC_CSR = 0x0c; // 选择通道12 ADC_TDRL = 0x20; } void adc_data() { unsigned int a; ADC_CR1 = 0x01; // CR1寄存器的最低位置1,使能ADC转换 for(a=0;a<200;a++); // 延时一段时间,至少7uS,保证ADC模块的上电完成 ADC_CR1 = ADC_CR1 | 0x01; // 再次将CR1寄存器的最低位置1,使能ADC转换 while((ADC_CSR & 0x80) == 0); // 等待ADC结束 a= ADC_DRH*4; // 读出ADC结果的高8位 a = (a + (ADC_DRL&0x03;))*5; ............................................................... ................................................................ ................................................................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_807315755

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值