ADD file via upload
This commit is contained in:
parent
2a1abfc0e8
commit
6043fbba9e
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 AIIT XUOS Lab
|
||||||
|
* XiUOS is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @brief Priv-shell Command definition
|
||||||
|
*
|
||||||
|
* @param _func Command function
|
||||||
|
* @param _desc Command description
|
||||||
|
* @param _attr Command attributes if need
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
// #include <user_api.h>
|
||||||
|
#include <transform.h>
|
||||||
|
// hash code beginning
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
#define HASHSIZE 12
|
||||||
|
#define NULLKEY -32768
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *elem;
|
||||||
|
int count;
|
||||||
|
}HashTable;
|
||||||
|
int m=0;
|
||||||
|
|
||||||
|
//初始化散列表
|
||||||
|
int InitHashTable(HashTable *H)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
m=HASHSIZE;
|
||||||
|
H->count=m;
|
||||||
|
H->elem=(int*)malloc(m*sizeof(int));
|
||||||
|
for(i=0;i<m;i++)
|
||||||
|
H->elem[i]=NULLKEY;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//散列函数
|
||||||
|
int Hash(int key)
|
||||||
|
{
|
||||||
|
return key%m;
|
||||||
|
}
|
||||||
|
//插入关键字进入散列表
|
||||||
|
void InsertHash(HashTable *H,int key)
|
||||||
|
{
|
||||||
|
int addr=Hash(key);
|
||||||
|
while(H->elem[addr]!=NULLKEY)
|
||||||
|
addr=(addr+1)%m;
|
||||||
|
H->elem[addr]=key;
|
||||||
|
}
|
||||||
|
|
||||||
|
//散列表查找关键字
|
||||||
|
int SearchHash(HashTable H,int key,int *addr)
|
||||||
|
{
|
||||||
|
*addr=Hash(key);
|
||||||
|
while(H.elem[*addr]!=key)
|
||||||
|
{
|
||||||
|
*addr=(*addr+1)%m;
|
||||||
|
if(H.elem[*addr]==NULLKEY||*addr==Hash(key))
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
extern int FrameworkInit();
|
||||||
|
extern void ApplicationOtaTaskInit(void);
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
int a[12]={12,67,56,16,25,37,22,29,15,47,48,34};
|
||||||
|
HashTable H;
|
||||||
|
int i;
|
||||||
|
InitHashTable(&H);
|
||||||
|
printf("The initial array is as follows:\n");
|
||||||
|
for(i=0;i<m;i++)
|
||||||
|
printf("%d,",a[i]);
|
||||||
|
for(i=0;i<m;i++)
|
||||||
|
InsertHash(&H,a[i]);
|
||||||
|
printf("\nThe resulting hash table is:\n");
|
||||||
|
for(i=0;i<m;i++)
|
||||||
|
{
|
||||||
|
printf("%d,",H.elem[i]);
|
||||||
|
}
|
||||||
|
int addr,j;
|
||||||
|
j=SearchHash(H,a[5],&addr);
|
||||||
|
|
||||||
|
printf("搜索到a[5]的地址是:%d\n",j);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FrameworkInit();
|
||||||
|
#ifdef APPLICATION_OTA
|
||||||
|
ApplicationOtaTaskInit();
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// int cppmain(void);
|
Loading…
Reference in New Issue