From 846c8e5d3098a8366dbca0435abd2bcb5d7c9044 Mon Sep 17 00:00:00 2001 From: S7WXC <1159864034@qq.com> Date: Thu, 5 Oct 2023 15:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=83=AD=E8=BA=AB=E8=B5=9B-=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APP_Framework/Applications/main.c | 71 +++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/APP_Framework/Applications/main.c b/APP_Framework/Applications/main.c index e1de024b5..0e0da3787 100644 --- a/APP_Framework/Applications/main.c +++ b/APP_Framework/Applications/main.c @@ -11,16 +11,81 @@ */ #include +#include #include // #include #include - +#define MAX 3 // MAX为哈希表的大小,定义为3 extern int FrameworkInit(); extern void ApplicationOtaTaskInit(void); -int main(void) + +typedef struct node //构建结构体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; + } +} + +int main(void) +{ + NODE *M, *p; //声明两个指针,用于操作哈希表和遍历链表 + int n = 5, second, i = 0; //定义五个数 + int first; + M = (NODE *)malloc(sizeof(NODE) * MAX); + Hash(M); //初始化哈希表 + CreateHash(M, 1, 50); //将(1,50)插入哈希表中 + CreateHash(M, 2, 40); + CreateHash(M, 3, 70); + CreateHash(M, 4, 60); + CreateHash(M, 5, 100); + printf("number: 1,50\n"); + printf("number: 2,40\n"); + printf("number: 3,70\n"); + printf("number: 4,60\n"); + printf("number: 5,100\n"); + for (i = 0; i < MAX; i++) //循环遍历哈希表的所有位置 + { + printf("Hash Slot %d:", i); + p = M[i].next; //将指针p指向当前位置的链表头 + while (p != NULL) + { + printf("->%d", p->second); + p = p->next; //将指针p移动到链表的下一节点 + } + printf("\n"); + } + FrameworkInit(); #ifdef APPLICATION_OTA ApplicationOtaTaskInit();