hash
This commit is contained in:
parent
1a6ee0234b
commit
f1d2db8cbb
|
@ -11,16 +11,152 @@
|
|||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
// #include <user_api.h>
|
||||
#include <transform.h>
|
||||
|
||||
|
||||
extern int FrameworkInit();
|
||||
extern void ApplicationOtaTaskInit(void);
|
||||
int main(void)
|
||||
|
||||
#define MAX 7
|
||||
typedef struct node
|
||||
{
|
||||
printf("Hello, world! \n");
|
||||
int first;
|
||||
int second;
|
||||
struct node *next;
|
||||
} NODE;
|
||||
void Hash(NODE *M)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
M[i].next = NULL;
|
||||
}
|
||||
}
|
||||
void CreateHash(NODE *M, int first, int second)
|
||||
{
|
||||
NODE *s;
|
||||
s = (NODE *)malloc(sizeof(NODE));
|
||||
s->first = first;
|
||||
s->second = second;
|
||||
s->next = NULL;
|
||||
int index = first % MAX;
|
||||
if (M[index].next == NULL)
|
||||
{
|
||||
M[index].next = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
NODE *tail = M[index].next;
|
||||
while (tail->next != NULL)
|
||||
{
|
||||
tail = tail->next;
|
||||
}
|
||||
tail->next = s;
|
||||
}
|
||||
}
|
||||
void DeleteNodeHash(NODE *M, int first)
|
||||
{
|
||||
int index = first % MAX;
|
||||
NODE *current = M[index].next;
|
||||
NODE *prev = NULL;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current->first == first)
|
||||
{
|
||||
if (prev == NULL)
|
||||
{
|
||||
M[index].next = current->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev->next = current->next;
|
||||
}
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
void DeleteHashTable(NODE *M)
|
||||
{
|
||||
for (int i = 0; i < MAX; i++)
|
||||
{
|
||||
NODE *current = M[i].next;
|
||||
while (current != NULL)
|
||||
{
|
||||
NODE *temp = current;
|
||||
current = current->next;
|
||||
free(temp);
|
||||
}
|
||||
M[i].next = NULL;
|
||||
}
|
||||
}
|
||||
void InsertNodeHash(NODE *M, int first, int second)
|
||||
{
|
||||
NODE *s = (NODE *)malloc(sizeof(NODE));
|
||||
s->first = first;
|
||||
s->second = second;
|
||||
s->next = NULL;
|
||||
int index = first % MAX;
|
||||
|
||||
if (M[index].next == NULL)
|
||||
{
|
||||
M[index].next = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
s->next = M[index].next;
|
||||
M[index].next = s;
|
||||
}
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
NODE *M, *p;
|
||||
int n = 5, second, i = 0;
|
||||
int first;
|
||||
M = (NODE *)malloc(sizeof(NODE) * MAX);
|
||||
Hash(M);
|
||||
InsertNodeHash(M, 1, 20);
|
||||
InsertNodeHash(M, 2, 40);
|
||||
InsertNodeHash(M, 8, 8);
|
||||
InsertNodeHash(M, 9, 7);
|
||||
printf("number: 1,20\n");
|
||||
printf("number: 2,40\n");
|
||||
printf("number: 8,8\n");
|
||||
printf("number: 9,7\n");
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
printf("Hash Slot %d:", i);
|
||||
p = M[i].next;
|
||||
while (p != NULL)
|
||||
{
|
||||
printf("->%d", p->second);
|
||||
p = p->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
DeleteNodeHash(M, 8);
|
||||
printf("Delete number: 8,8\n");
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
printf("Hash Slot %d:", i);
|
||||
p = M[i].next;
|
||||
while (p != NULL)
|
||||
{
|
||||
printf("->%d", p->second);
|
||||
p = p->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
DeleteHashTable(M);
|
||||
printf("Delete HashTable");
|
||||
|
||||
|
||||
FrameworkInit();
|
||||
#ifdef APPLICATION_OTA
|
||||
ApplicationOtaTaskInit();
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
## 1. 简介
|
||||
热身赛赛题一哈希表
|
||||
|
||||
## 2. 数据结构设计说明
|
||||
哈希表基本功能实现,大小为7,以链表方式实现,插入出现冲突时以头插法插入对应表头;
|
||||
删除对应数据时到对应链表中删除结点,结点定义如下,first值要求唯一:
|
||||
typedef struct node
|
||||
{
|
||||
int first;
|
||||
int second;
|
||||
struct node *next;
|
||||
} NODE;
|
||||
|
||||
## 3. 测试程序说明
|
||||
测试哈希表的创建、插入、删除:
|
||||
M = (NODE *)malloc(sizeof(NODE) * MAX);
|
||||
Hash(M);
|
||||
InsertNodeHash(M, 1, 20);
|
||||
InsertNodeHash(M, 2, 40);
|
||||
InsertNodeHash(M, 8, 8);
|
||||
InsertNodeHash(M, 9, 7);
|
||||
printf("number: 1,20\n");
|
||||
printf("number: 2,40\n");
|
||||
printf("number: 8,8\n");
|
||||
printf("number: 9,7\n");
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
printf("Hash Slot %d:", i);
|
||||
p = M[i].next;
|
||||
while (p != NULL)
|
||||
{
|
||||
printf("->%d", p->second);
|
||||
p = p->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
DeleteNodeHash(M, 8);
|
||||
printf("Delete number: 8,8\n");
|
||||
for (i = 0; i < MAX; i++)
|
||||
{
|
||||
printf("Hash Slot %d:", i);
|
||||
p = M[i].next;
|
||||
while (p != NULL)
|
||||
{
|
||||
printf("->%d", p->second);
|
||||
p = p->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
DeleteHashTable(M);
|
||||
printf("Delete HashTable");
|
||||
|
||||
## 4. 运行结果(##需结合运行测试截图按步骤说明##)
|
||||
烧录后产生结果如截图1.png
|
||||
|
Loading…
Reference in New Issue