写一个函数,来维护一组key、value的关系。 key:imsi value:imsi、imei、common.eci、common.tac、last_time 要求,imsi相同时,以最后一条记录的value内容为准进行保存。用C语言来写,数据结构用哈希完成

时间: 2024-03-23 19:44:07 浏览: 5
下面是用C语言实现的维护key-value关系的哈希表函数,满足题目要求: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STR_LEN 100 #define HASH_SIZE 100 // 定义key-value对的结构体 typedef struct { char *key; char *value; } KeyValue; // 定义哈希表的结构体 typedef struct { KeyValue **buckets; int size; } HashTable; // 初始化哈希表 void initHashTable(HashTable *table) { table->buckets = calloc(HASH_SIZE, sizeof(KeyValue*)); table->size = HASH_SIZE; } // 在哈希表中查找指定key的value char* lookupValue(HashTable *table, char *key) { int index = hash(key) % table->size; KeyValue *pair = table->buckets[index]; while (pair != NULL) { if (strcmp(pair->key, key) == 0) { return pair->value; } pair = pair->next; } return NULL; } // 在哈希表中添加或更新指定key的value void addOrUpdateValue(HashTable *table, char *key, char *value) { int index = hash(key) % table->size; KeyValue *pair = table->buckets[index]; KeyValue *lastPair = NULL; while (pair != NULL && strcmp(pair->key, key) != 0) { lastPair = pair; pair = pair->next; } if (pair != NULL) { free(pair->value); pair->value = strdup(value); } else { KeyValue *newPair = malloc(sizeof(KeyValue)); newPair->key = strdup(key); newPair->value = strdup(value); newPair->next = NULL; if (lastPair == NULL) { table->buckets[index] = newPair; } else { lastPair->next = newPair; } } } // 释放哈希表 void freeHashTable(HashTable *table) { for (int i = 0; i < table->size; i++) { KeyValue *pair = table->buckets[i]; while (pair != NULL) { KeyValue *nextPair = pair->next; free(pair->key); free(pair->value); free(pair); pair = nextPair; } } free(table->buckets); } // 哈希函数 unsigned long hash(char *str) { unsigned long hash = 5381; int c; while ((c = *str++)) { hash = ((hash << 5) + hash) + c; } return hash; } int main() { HashTable table; initHashTable(&table); // 模拟添加key-value对 addOrUpdateValue(&table, "imsi1", "imsi1,imei1,eci1,tac1,100"); addOrUpdateValue(&table, "imsi2", "imsi2,imei2,eci2,tac2,200"); addOrUpdateValue(&table, "imsi1", "imsi1,imei3,eci3,tac3,300"); addOrUpdateValue(&table, "imsi3", "imsi3,imei4,eci4,tac4,400"); // 打印哈希表中的key-value对 printf("imsi1: %s\n", lookupValue(&table, "imsi1")); printf("imsi2: %s\n", lookupValue(&table, "imsi2")); printf("imsi3: %s\n", lookupValue(&table, "imsi3")); // 释放哈希表 freeHashTable(&table); return 0; } ``` 这个程序中,我们使用了一个哈希表来存储key-value对。在哈希表中,我们使用`KeyValue`结构体来存储单个key-value对,`HashTable`结构体来存储整个哈希表。我们使用`initHashTable()`函数初始化哈希表,`lookupValue()`函数在哈希表中查找指定key的value,`addOrUpdateValue()`函数在哈希表中添加或更新指定key的value,`freeHashTable()`函数释放哈希表。在`main()`函数中,我们模拟添加了四个key-value对,并打印了哈希表中的所有value。哈希函数使用了经典的djb2哈希算法。由于题目要求imsi相同时,以最后一条记录的value内容为准进行保存,因此我们在`addOrUpdateValue()`函数中实现了这个功能。

相关推荐

void S1mmeSession::CtEncodeKqi(S1MMEKQI* kqi, S1APNode* p_node, uint8_t worker_id) { MsgCommonInfo& common = p_node->GetCommonInfo(); SPUserInfo& sp_user_info = p_node->GetUserInfo(); //获取 buf TlvEncoder* p_encoder_cur = g_p_encoder_[worker_id]; YdCDR_T* p_dst_data = (YdCDR_T*)malloc(sizeof(YdCDR_T)); if (p_dst_data == NULL) { return; } p_dst_data->not_associate = 0; if ((common.not_associate & 0x03) == 0x03) p_dst_data->not_associate = 1; p_encoder_cur->Set(p_dst_data->cdr_data,kMaxOneCdrBufLen); uint64_t imsi = sp_user_info->GetIMSI(); if(common.eci == 0) { common.eci = sp_user_info->GetEci(); } uint16_t tmp_enbid = common.tac;//>>8; //uint32_t tmp_enbid = (common.eci >> 8)&0xfffff; char xdrid_str[32]={0}; #ifdef OPEN_NEW_HUISU convert_xdrid_to_string(xdrid_str, kqi->xdrid, s_xdr_id_len); #else #ifdef OPENCTPR g4sigtran::pr::ProcBlock* p_blk = kqi->binary_block_in_xdr_.GetBlock(); p_blk->SerializeXid(xdrid_str, sizeof(xdrid_str)); #else uint64_t subcdrid = g_ct_xdr_id.GetXid(); //reverse subend; if(::is_open_reverse) { SetReverseSubend(p_node, subcdrid); } #ifdef ONE_THIRD_YUNNAN_MRO g_ct_xdr_id.Serialize((uint8_t*)xdrid_str, s_xdr_id_len, imsi); #else g_ct_xdr_id.Serialize((uint8_t*)xdrid_str, s_xdr_id_len); #endif #endif #endif struct timespec start_time = kqi->request_time_, end_time = kqi->response_time_; if (kqi->request_time_.tv_sec == 0) { if (!(kqi->response_time_.tv_sec == 0)) { start_time = kqi->response_time_; } else if (!(kqi->complete_time_.tv_sec == 0)) { start_time = kqi->complete_time_; } } 在S1mmeSession::CtEncodeKqi函数最后面加一个函数,来维护一组key、value的关系。 key:imsi value:imsi、imei、common.eci、common.tac、last_time 要求,imsi相同时,以最后一条记录的value内容为准进行保存。

void S1mmeSession::CtEncodeKqi(S1MMEKQI* kqi, S1APNode* p_node, uint8_t worker_id) { MsgCommonInfo& common = p_node->GetCommonInfo(); SPUserInfo& sp_user_info = p_node->GetUserInfo(); //获取 buf TlvEncoder* p_encoder_cur = g_p_encoder_[worker_id]; YdCDR_T* p_dst_data = (YdCDR_T*)malloc(sizeof(YdCDR_T)); if (p_dst_data == NULL) { return; } p_dst_data->not_associate = 0; if ((common.not_associate & 0x03) == 0x03) p_dst_data->not_associate = 1; p_encoder_cur->Set(p_dst_data->cdr_data,kMaxOneCdrBufLen); uint64_t imsi = sp_user_info->GetIMSI(); if(common.eci == 0) { common.eci = sp_user_info->GetEci(); } uint16_t tmp_enbid = common.tac;//>>8; //uint32_t tmp_enbid = (common.eci >> 8)&0xfffff; char xdrid_str[32]={0}; #ifdef OPEN_NEW_HUISU convert_xdrid_to_string(xdrid_str, kqi->xdrid, s_xdr_id_len); #else #ifdef OPENCTPR g4sigtran::pr::ProcBlock* p_blk = kqi->binary_block_in_xdr_.GetBlock(); p_blk->SerializeXid(xdrid_str, sizeof(xdrid_str)); #else uint64_t subcdrid = g_ct_xdr_id.GetXid(); //reverse subend; if(::is_open_reverse) { SetReverseSubend(p_node, subcdrid); } #ifdef ONE_THIRD_YUNNAN_MRO g_ct_xdr_id.Serialize((uint8_t*)xdrid_str, s_xdr_id_len, imsi); #else g_ct_xdr_id.Serialize((uint8_t*)xdrid_str, s_xdr_id_len); #endif #endif #endif struct timespec start_time = kqi->request_time_, end_time = kqi->response_time_; if (kqi->request_time_.tv_sec == 0) { if (!(kqi->response_time_.tv_sec == 0)) { start_time = kqi->response_time_; } else if (!(kqi->complete_time_.tv_sec == 0)) { start_time = kqi->complete_time_; } } if (!(kqi->complete_time_.tv_sec == 0)) { end_time = kqi->complete_time_; } if (end_time.tv_sec == 0) { end_time = start_time; } p_encoder_cur->SetHdr(kEncoderCdr, kqi->kqi_type_, current_time_.tv_sec, worker_id);在S1mmeSession::CtEncodeKqi函数最后面加一个函数,来维护一组key、value的关系。 key:imsi value:imsi、imei、common.eci、common.tac、last_time 要求,imsi相同时,以最后一条记录的value内容为准进行保存。

void S1mmeSession::CtEncodeKqi(S1MMEKQI* kqi, S1APNode* p_node, uint8_t worker_id) { MsgCommonInfo& common = p_node->GetCommonInfo(); SPUserInfo& sp_user_info = p_node->GetUserInfo(); //获取 buf TlvEncoder* p_encoder_cur = g_p_encoder_[worker_id]; YdCDR_T* p_dst_data = (YdCDR_T*)malloc(sizeof(YdCDR_T)); if (p_dst_data == NULL) { return; } p_dst_data->not_associate = 0; if ((common.not_associate & 0x03) == 0x03) p_dst_data->not_associate = 1; p_encoder_cur->Set(p_dst_data->cdr_data,kMaxOneCdrBufLen); uint64_t imsi = sp_user_info->GetIMSI(); if(common.eci == 0) { common.eci = sp_user_info->GetEci(); } uint16_t tmp_enbid = common.tac;//>>8; //uint32_t tmp_enbid = (common.eci >> 8)&0xfffff; char xdrid_str[32]={0}; #ifdef OPEN_NEW_HUISU convert_xdrid_to_string(xdrid_str, kqi->xdrid, s_xdr_id_len); #else #ifdef OPENCTPR g4sigtran::pr::ProcBlock* p_blk = kqi->binary_block_in_xdr_.GetBlock(); p_blk->SerializeXid(xdrid_str, sizeof(xdrid_str)); #else uint64_t subcdrid = g_ct_xdr_id.GetXid(); //reverse subend; if(::is_open_reverse) { SetReverseSubend(p_node, subcdrid); } #ifdef ONE_THIRD_YUNNAN_MRO g_ct_xdr_id.Serialize((uint8_t*)xdrid_str, s_xdr_id_len, imsi); #else g_ct_xdr_id.Serialize((uint8_t*)xdrid_str, s_xdr_id_len); #endif #endif #endif struct timespec start_time = kqi->request_time_, end_time = kqi->response_time_; if (kqi->request_time_.tv_sec == 0) { if (!(kqi->response_time_.tv_sec == 0)) { start_time = kqi->response_time_; } else if (!(kqi->complete_time_.tv_sec == 0)) { start_time = kqi->complete_time_; } }要求:在S1mmeSession::CtEncodeKqi函数后面新加一个函数,来维护一组key、value的关系。 key为:imsi value为:imsi、imei、common.eci、common.tac、last_time 当imsi相同时,以最后一条记录的value内容为准进行保存;imsi不同时直接插入。请用C++实现该功能,需要用到哈希的知识点

最新推荐

recommend-type

详解关于AndroidQ获取不到imsi解决方案

主要介绍了详解关于AndroidQ获取不到imsi解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

电脑查看终端IMSI号方法(XP系统).docx

通过该方法快速查询终端IMSI号码,暂时只支持XP系统,其他系统无法实现。
recommend-type

android平台获取手机IMSI,IMEI ,序列号,和 手机号的方法

android平台获取手机IMSI,IMEI ,序列号,和 手机号的方法
recommend-type

网络编程网络编程网络编程

网络编程网络编程网络编程网络编程
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依