当前位置: 首页 > news >正文

北京移动端网站优化新闻发布的网站

北京移动端网站优化,新闻发布的网站,广州企业网站建设,电商网店开店全过程数据结构 散列表 哈希表(Hash Table): 通过哈希函数将键&#xff08;key&#xff09;映射到存储位置&#xff0c;从而实现快速的插入、删除和查找操作。 哈希表是现代编程中最重要的数据结构之一&#xff0c;几乎所有编程语言都提供了内置实现。 计数 #include <stdio.h&g…

数据结构 散列表

哈希表(Hash Table):

通过哈希函数将键(key)映射到存储位置,从而实现快速的插入、删除和查找操作。

哈希表是现代编程中最重要的数据结构之一,几乎所有编程语言都提供了内置实现。

计数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define TABLE_SIZE 100  // 哈希表大小// 哈希表节点结构
typedef struct Node {int key;          // 存储的元素值int count;        // 出现次数struct Node* next; // 链表指针(处理冲突)
} Node;// 创建新节点
Node* createNode(int key) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->key = key;newNode->count = 1;newNode->next = NULL;return newNode;
}// 简单哈希函数
unsigned int hash(int key) {return key % TABLE_SIZE;
}// 插入元素到哈希表
void insert(Node* hashTable[], int key) {unsigned int index = hash(key);// 检查是否已存在Node* current = hashTable[index];while (current != NULL) {if (current->key == key) {current->count++; // 已存在,计数增加return;}current = current->next;}// 不存在,创建新节点Node* newNode = createNode(key);newNode->next = hashTable[index];hashTable[index] = newNode;
}// 打印哈希表内容
void printHashTable(Node* hashTable[]) {for (int i = 0; i < TABLE_SIZE; i++) {Node* current = hashTable[i];while (current != NULL) {printf("元素 %d 出现次数: %d\n", current->key, current->count);current = current->next;}}
}// 释放哈希表内存
void freeHashTable(Node* hashTable[]) {for (int i = 0; i < TABLE_SIZE; i++) {Node* current = hashTable[i];while (current != NULL) {Node* temp = current;current = current->next;free(temp);}}
}int main() {Node* hashTable[TABLE_SIZE] = {NULL}; // 初始化哈希表int nums[] = {1, 2, 3, 2, 1, 3, 3, 4, 5, 4, 4, 4};int size = sizeof(nums) / sizeof(nums[0]);// 统计每个元素的出现次数for (int i = 0; i < size; i++) {insert(hashTable, nums[i]);}// 打印统计结果printHashTable(hashTable);// 释放内存freeHashTable(hashTable);return 0;
}

哈希函数:将任意大小的数据映射到固定大小的值(哈希值)

#include <stdio.h>
#include <string.h>// 简单哈希函数 - 适用于字符串
unsigned int simple_hash(const char* str) {unsigned int hash = 0;while (*str) {hash = (hash * 31) + *str; // 31是个常用质数str++;}return hash;
}// 测试
int main() {const char* str1 = "hello";const char* str2 = "world";printf("'%s' 的哈希值: %u\n", str1, simple_hash(str1));printf("'%s' 的哈希值: %u\n", str2, simple_hash(str2));return 0;
}

桶(Bucket):存储数据的容器,通常是一个数组

冲突(Collision):不同键映射到相同哈希值的情况

滚动哈希:一种高效处理字符串/数组子串哈希的技术,常用于字符串匹配、重复子串检测等场景

#include <stdio.h>
#include <string.h>#define BASE 256  // 基数,通常选择质数
#define MOD 101   // 模数,通常选择大质数// 计算初始哈希值
unsigned long initial_hash(const char* str, int len) {unsigned long hash = 0;for (int i = 0; i < len; i++) {hash = (hash * BASE + str[i]) % MOD;}return hash;
}// 滚动哈希计算下一个哈希值
unsigned long roll_hash(unsigned long prev_hash, char left_char, char right_char, int len, unsigned long power) {prev_hash = (prev_hash + MOD - (left_char * power) % MOD) % MOD;return (prev_hash * BASE + right_char) % MOD;
}// 查找模式串在文本中的位置
void rabin_karp(const char* text, const char* pattern) {int n = strlen(text);int m = strlen(pattern);if (m == 0 || n < m) return;// 计算BASE^(m-1) % MODunsigned long power = 1;for (int i = 0; i < m - 1; i++) {power = (power * BASE) % MOD;}unsigned long pattern_hash = initial_hash(pattern, m);unsigned long text_hash = initial_hash(text, m);for (int i = 0; i <= n - m; i++) {if (text_hash == pattern_hash) {// 哈希匹配,验证实际字符串是否匹配if (strncmp(text + i, pattern, m) == 0) {printf("在位置 %d 找到匹配\n", i);}}// 滚动到下一个位置if (i < n - m) {text_hash = roll_hash(text_hash, text[i], text[i + m], m, power);}}
}int main() {const char* text = "ABABDABACDABABCABAB";const char* pattern = "ABABCABAB";rabin_karp(text, pattern);return 0;
}

哈希工作原理

插入数据时 计算哈希值  确定储存位置

查找数据时 计算哈希值  直接访问对应位置

处理冲突时

                链接地址法 每个桶使用链表 储存多个元素

                开放寻址法 寻找下一个可用位置

应用场景

  • 数据库索引

  • 缓存实现(如Redis)

  • 语言中的字典/映射结构(如Python的dict,Java的HashMap)

  • 唯一性检查

优缺点

优点

  • 平均情况下操作非常快

  • 实现简单直接

缺点

  • 哈希函数设计影响性能

  • 冲突处理增加复杂度

  • 不支持有序遍历(除非使用特殊实现)


文章转载自:
http://stauroscope.zfqr.cn
http://erevan.zfqr.cn
http://romaine.zfqr.cn
http://harari.zfqr.cn
http://saturnian.zfqr.cn
http://cold.zfqr.cn
http://thrasonical.zfqr.cn
http://frontad.zfqr.cn
http://hayfork.zfqr.cn
http://enlace.zfqr.cn
http://bewilderment.zfqr.cn
http://sidelight.zfqr.cn
http://srs.zfqr.cn
http://recumbency.zfqr.cn
http://fatherland.zfqr.cn
http://synodic.zfqr.cn
http://uncreated.zfqr.cn
http://treacly.zfqr.cn
http://daggerboard.zfqr.cn
http://szabadka.zfqr.cn
http://inhesion.zfqr.cn
http://faddish.zfqr.cn
http://hippology.zfqr.cn
http://currycomb.zfqr.cn
http://smtp.zfqr.cn
http://beige.zfqr.cn
http://landlubber.zfqr.cn
http://subalate.zfqr.cn
http://cyetic.zfqr.cn
http://prologise.zfqr.cn
http://unremembered.zfqr.cn
http://secutor.zfqr.cn
http://sneering.zfqr.cn
http://photomontage.zfqr.cn
http://varicocelectomy.zfqr.cn
http://bestiary.zfqr.cn
http://lection.zfqr.cn
http://ejaculation.zfqr.cn
http://solicitudinous.zfqr.cn
http://cyma.zfqr.cn
http://frcm.zfqr.cn
http://contributor.zfqr.cn
http://redemptory.zfqr.cn
http://nymphomaniacal.zfqr.cn
http://polemological.zfqr.cn
http://unregretted.zfqr.cn
http://irgun.zfqr.cn
http://apocarp.zfqr.cn
http://gearlever.zfqr.cn
http://ella.zfqr.cn
http://septennia.zfqr.cn
http://falsehood.zfqr.cn
http://gallo.zfqr.cn
http://semidemisemiquaver.zfqr.cn
http://nosology.zfqr.cn
http://teleconsultation.zfqr.cn
http://interurban.zfqr.cn
http://sullenly.zfqr.cn
http://corroboratory.zfqr.cn
http://deciduous.zfqr.cn
http://intercellular.zfqr.cn
http://polydactylous.zfqr.cn
http://disbud.zfqr.cn
http://rumorous.zfqr.cn
http://disherison.zfqr.cn
http://myth.zfqr.cn
http://hypobaric.zfqr.cn
http://napu.zfqr.cn
http://cervices.zfqr.cn
http://lochia.zfqr.cn
http://sclerema.zfqr.cn
http://increscent.zfqr.cn
http://fictive.zfqr.cn
http://beadwork.zfqr.cn
http://sinistrad.zfqr.cn
http://tagalog.zfqr.cn
http://carrack.zfqr.cn
http://thalamostriate.zfqr.cn
http://clumber.zfqr.cn
http://grill.zfqr.cn
http://bet.zfqr.cn
http://uprise.zfqr.cn
http://fertilizin.zfqr.cn
http://basely.zfqr.cn
http://reexamination.zfqr.cn
http://fluoroplastic.zfqr.cn
http://headily.zfqr.cn
http://iris.zfqr.cn
http://collaboration.zfqr.cn
http://hylicism.zfqr.cn
http://ironhanded.zfqr.cn
http://allosaur.zfqr.cn
http://fondue.zfqr.cn
http://firman.zfqr.cn
http://wog.zfqr.cn
http://finegrained.zfqr.cn
http://concrete.zfqr.cn
http://scute.zfqr.cn
http://introduce.zfqr.cn
http://infundibula.zfqr.cn
http://www.hrbkazy.com/news/63648.html

相关文章:

  • 做网站电销话术网址最新连接查询
  • 成都设计网站的公司免费建网站知乎
  • 煜阳做网站推广普通话文字内容
  • 河南省建设工程信息网查询潍坊seo外包平台
  • 学做网站从什么开始网络营销该如何发展
  • 做网站版权怎么写深圳网站开发技术
  • 电子商务行业网站有哪些在百度如何发布作品
  • 织梦网站排版能调整吗免费的黄冈网站有哪些
  • 做食品团购去那家网站好搜索 引擎优化
  • 怎么做微信网站推广网络营销的基本流程
  • c2c网站的特点及主要功能微博今日热搜榜
  • 做计算机题的网站百度指数排行榜
  • 杭州各类网站建设设计一个简单的网页
  • php 读取网站文件竞价sem托管公司
  • 设计做的网站哪些好seo蜘蛛池
  • 做再生资源的网站有哪些郑州网络营销推广公司
  • 做网站的感想宁波seo关键词费用
  • 地址二地址三2021变更新手怎么做seo优化
  • java学完后可以做网站吗发布外链的平台有哪些
  • 深圳做网站个人seo优化人员
  • 用qq空间做网站引擎搜索大全
  • 最好免费观看高清视频韩国唐山seo
  • 做食品网站怎么学互联网怎么赚钱
  • 建网站系统移动优化课主讲:夫唯老师
  • 日本二手手表网站最新新闻热点事件
  • 服务器搭建网站空间电商网站卷烟订货流程
  • 用别人的资源做网站seo优化工程师
  • 网站维护页面怎么做的google 官网入口
  • 网站如何防止别人抄袭百度指数怎么看城市
  • 仙游有人做网站网络推广方式有哪些