"cortex-m3哈希表测试"

This commit is contained in:
pgh_dd 2023-07-25 23:01:31 +08:00
parent 7601ef45fe
commit 4839348111
7 changed files with 215 additions and 0 deletions

View File

@ -240,5 +240,9 @@ menu "test app"
bool "Config test soft timer"
default n
menuconfig USER_TEST_HASHTABLE
bool "Config test hash"
default n
endif
endmenu

View File

@ -101,5 +101,8 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
SRC_FILES += test_timer.c
endif
ifeq ($(CONFIG_USER_TEST_HASHTABLE),y)
SRC_DIR += test_hash
endif
include $(KERNEL_ROOT)/compiler.mk
endif

View File

@ -0,0 +1,12 @@
ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
SRC_FILES := hash.c test_hash.c
include $(KERNEL_ROOT)/compiler.mk
endif
include $(KERNEL_ROOT)/.config
ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
include $(APPDIR)/Make.defs
CSRCS += hash.c test_hash.c
include $(APPDIR)/Application.mk
endif

View File

@ -0,0 +1,112 @@
#include<stdio.h>
#include<stdlib.h>
#include"hash.h"
// 通常是质数
int Hash(int value)
{
return value%REMAINDER;
}
HashTableType* CreatTable(int table_size)
{
HashTableType *h = (HashTableType *)malloc(sizeof(HashTableType));
h->size = REMAINDER;
h->head = (Node *)malloc((h->size)*sizeof(Node));
h->length = 0;
int i = 0;
for(i=0 ; i<h->size ; i++)
{
h->head[i].next = NULL;
}
return h;
}
Node *LookUp(HashTableType *h , int key)
{
int i;
i = Hash(key);
Node * p = h->head[i].next;
while(p && key != p->data->key)
{
p = p->next;
}
return p;
}
void Insert(HashTableType *h , ElementType k)
{
Node * p = LookUp(h,k.key);
if(!p)
{
Node *q = (Node *)malloc(sizeof(Node));
q->data = (ElementType *)malloc(sizeof(ElementType));
(q->data)->key = k.key;
(q->data)->value = k.value;
int position;
position = (q->data)->hash = Hash(k.key);
q->next = h->head[position].next;
h->head[position].next = q;
h->length += 1;
return ;
}
else
{
printf("The keys is exist !\n");
return ;
}
}
void DestroyTable(HashTableType *h)
{
int i;
Node *p , *q;
for(i=0 ; i<h->size ; i++)
{
p = h->head[i].next;
while(p)
{
q=p->next;
free(p);
p=q;
}
}
free(h->head);
free(h);
}
void PrintTable(HashTableType *h)
{
int i = 0;
for (i = 0; i < h->size ; i++)
{
Node * p = h->head[i].next;
while (p)
{
printf("[%d-%d] ",p->data->key, p->data->value);
p = p->next;
}
printf("NULL\n");
}
}
void Test()
{
ElementType a[]={{12,1},{2,2},{31,3},{45,4},{8,5}};
int n = sizeof(a)/sizeof(ElementType);
HashTableType *h = CreatTable(n);
int i = 0;
for(i = 0 ; i<n ; i++)
{
Insert(h,a[i]);
}
PrintTable(h); // 打印哈希表
printf("%d\n\n", LookUp(h,12)->data->key); //查找key值为12的Element
printf("%d\n",h->length); //打印哈希表的元素个数
DestroyTable(h); // 摧毁哈希表
}

View File

@ -0,0 +1,48 @@
/*
* @Description:
* @Version: V1.0.0
* @Author:
* @Date: 2023-07-25 22:26:04
* @LastEditors: pgh_dd 1041315949@qq.com
* @LastEditTime: 2023-07-25 22:39:54
*/
#ifndef __HASH_H__
#define __HASH_H__
#include <stdio.h>
#define REMAINDER 11
typedef struct ElementType
{
int key;
int value;
int hash;
}ElementType;
typedef struct Pnode
{
ElementType *data;
struct Pnode *next;
}Node;
typedef struct HashTable
{
int size;
int length;
struct Pnode *head;
}HashTableType;
int Hash(int value);
HashTableType* CreatTable(int table_size);
Node *LookUp(HashTableType *h , int key);
void Insert(HashTableType *h , ElementType k);
void DestroyTable(HashTableType *h);
void PrintTable(HashTableType *h);
void Test();
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

@ -0,0 +1,36 @@
/*
* @Description:
* @Version: V1.0.0
* @Author:
* @Date: 2023-07-25 22:26:04
* @LastEditors: pgh_dd 1041315949@qq.com
* @LastEditTime: 2023-07-25 22:40:28
*/
/*
* 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.
*/
/**
* @file: test_shell.c
* @brief: a application of shell function
* @version: 2.0
* @author: AIIT XUOS Lab
* @date: 2022/9/26
*/
#include <transform.h>
#include "hash.h"
void TestHash(void)
{
Test();
}
PRIV_SHELL_CMD_FUNCTION(TestHash, a hash sample , PRIV_SHELL_CMD_MAIN_ATTR);