This commit is contained in:
zyj879580 2023-06-23 17:28:37 +08:00
parent 09c866e878
commit 5984c699bf
6 changed files with 143 additions and 143 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -1,144 +1,144 @@
/*
* Copyright (c) 2020 AIIT Ubiquitous Team
* 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_hash.c
* @brief cortex-m3-emulator
* @description
* @version 1.0
* @author
* @date 2023-06-21
*************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<transform.h>
#ifdef ADD_XIZI_FETURES
/*定义哈希表所能存储结点个数*/
#define MAX_SIZE 10000L
/* 哈希表链节点实现数据结构 */
/* key 关键字 */
/* next 该节点指向的下一个节点 */
struct HashMap {
int key;
struct HashMap *next;
};
/* 哈希表节点数组,存储所有节点 */
struct HashMap *hash_map[MAX_SIZE];
/**
* @Description
* @param head -
* @param key -
* @return
*/
struct HashMap *InsertKey(struct HashMap *head, int key)
{
/* 将关键字组装为待插入的链节点 */
struct HashMap *insert_node = (struct HashMap *) malloc(sizeof(struct HashMap));
insert_node->key = key;
/* 如果节点为空则直接插入到head后,反之则根据除留余数法移动到合适位置 */
if (head == NULL) {
head = insert_node;
insert_node->next = NULL;
} else {
/* 用于while查询位置使用的指针 */
struct HashMap * p1 = head;
struct HashMap * p2 = NULL;
while ((insert_node->key > p1->key) && (p1->next != NULL)){
p2 = p1;
p1 = p1->next;
}
if (insert_node->key <= p1->key) {
/* 插入到第一个节点之前,否则插入到p2指向的节点之后 */
if (head == p1)
head = insert_node;
else
p2->next = insert_node;
insert_node->next = p1;
} else{
/* 插入到结尾处 */
p1->next = insert_node;
insert_node->next = NULL;
}
}
return head;
}
/**
* @Description InsertKey函数插入关键字构造哈希表
* @param size -
* @return
*/
void CreateHashMap(int size) {
for (int i = 0; i < size; i++)
hash_map[i] = NULL;
for (int i = 0; i < size; i++) {
int key;
scanf("%d", &key);
/* 吸收回车符 */
getchar();
/* 采用除留余数法确定位置 */
int location = key % (size + 1);
hash_map[location] = InsertKey(hash_map[location], key);
}
}
/**
* @Description
* @param head -
*/
void PrintHashMap(struct HashMap *head)
{
struct HashMap *p = head;
if(head != NULL){
do{
printf(" -> %d ",p->key);
p = p->next;
} while(p != NULL);
} else{
printf("null");
}
}
/**
* @Description
* @param size -
*/
void TestHash(void)
{
int size;
scanf("%d",&size);
getchar();
CreateHashMap(size);
printf("采用链地址法得到的哈希表为:\n");
for (int i = 0; i < size + 1; i++) {
printf("第%d行", i);
PrintHashMap(hash_map[i]);
printf("\n");
}
return ;
}
PRIV_SHELL_CMD_FUNCTION(TestHash, a hash_map test sample, PRIV_SHELL_CMD_MAIN_ATTR);
/*
* Copyright (c) 2020 AIIT Ubiquitous Team
* 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_hash.c
* @brief cortex-m3-emulator
* @description
* @version 1.0
* @author
* @date 2023-06-21
*************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<transform.h>
#ifdef ADD_XIZI_FETURES
/*定义哈希表所能存储结点个数*/
#define MAX_SIZE 10000L
/* 哈希表链节点实现数据结构 */
/* key 关键字 */
/* next 该节点指向的下一个节点 */
struct HashMap {
int key;
struct HashMap *next;
};
/* 哈希表节点数组,存储所有节点 */
struct HashMap *hash_map[MAX_SIZE];
/**
* @Description
* @param head -
* @param key -
* @return
*/
struct HashMap *InsertKey(struct HashMap *head, int key)
{
/* 将关键字组装为待插入的链节点 */
struct HashMap *insert_node = (struct HashMap *) malloc(sizeof(struct HashMap));
insert_node->key = key;
/* 如果节点为空则直接插入到head后,反之则根据除留余数法移动到合适位置 */
if (head == NULL) {
head = insert_node;
insert_node->next = NULL;
} else {
/* 用于while查询位置使用的指针 */
struct HashMap * p1 = head;
struct HashMap * p2 = NULL;
while ((insert_node->key > p1->key) && (p1->next != NULL)){
p2 = p1;
p1 = p1->next;
}
if (insert_node->key <= p1->key) {
/* 插入到第一个节点之前,否则插入到p2指向的节点之后 */
if (head == p1)
head = insert_node;
else
p2->next = insert_node;
insert_node->next = p1;
} else{
/* 插入到结尾处 */
p1->next = insert_node;
insert_node->next = NULL;
}
}
return head;
}
/**
* @Description InsertKey函数插入关键字构造哈希表
* @param size -
* @return
*/
void CreateHashMap(int size) {
for (int i = 0; i < size; i++)
hash_map[i] = NULL;
for (int i = 0; i < size; i++) {
int key;
scanf("%d", &key);
/* 吸收回车符 */
getchar();
/* 采用除留余数法确定位置 */
int location = key % (size + 1);
hash_map[location] = InsertKey(hash_map[location], key);
}
}
/**
* @Description
* @param head -
*/
void PrintHashMap(struct HashMap *head)
{
struct HashMap *p = head;
if(head != NULL){
do{
printf(" -> %d ",p->key);
p = p->next;
} while(p != NULL);
} else{
printf("null");
}
}
/**
* @Description
* @param size -
*/
void TestHash(void)
{
int size;
scanf("%d",&size);
getchar();
CreateHashMap(size);
printf("采用链地址法得到的哈希表为:\n");
for (int i = 0; i < size + 1; i++) {
printf("第%d行", i);
PrintHashMap(hash_map[i]);
printf("\n");
}
return ;
}
PRIV_SHELL_CMD_FUNCTION(TestHash, a hash_map test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif