C语言 C图数据结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5658248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:23:23  来源:igfitidea点击:

C map data structure

chashmap

提问by Udara S.S Liyanage

I want to implement a key value pair data structure in C. Any idea?

我想在 C 中实现一个键值对数据结构。知道吗?

回答by Carlos Daniel Gadea Omelchenko

Thisis a simple hash table implementation in ANSI C. It supports the rudimentary functions generally expected of a hash table:

是一个简单的 ANSI C 哈希表实现。它支持哈希表的基本功能:

  • Inserting and retrieving key-value associations
  • Querying the existence of a key
  • Returning the total number of key-value associations
  • Enumerating over all key-value associations
  • 插入和检索键值关联
  • 查询key是否存在
  • 返回键值关联的总数
  • 枚举所有键值关联

Hope this helps!

希望这可以帮助!

回答by sitaram chhimpa

If your key and value part are same data type then you can use a 2-D array with 2 column where first column will be your key and second will be data. IT will behave as map but time complexity will diff. Time complexity : search - O(n) Insert - want to maintain unique key then O(n) else O(1).

如果您的键和值部分是相同的数据类型,那么您可以使用具有 2 列的二维数组,其中第一列将是您的键,第二列将是数据。IT 将表现为地图,但时间复杂度会有所不同。时间复杂度:搜索 - O(n) 插入 - 想要维护唯一键然后 O(n) 否则 O(1)。

int map[N][2];

if you want to have key-value pair different type then you can use list structure.

如果你想拥有不同类型的键值对,那么你可以使用列表结构。

struct node
{
int key;  //key part
string value;  // value part
struct node *next;
};

Time complexity : search - O(n) Insert - want to maintain unique key then O(n) else O(1).

时间复杂度:搜索 - O(n) 插入 - 想要维护唯一键然后 O(n) 否则 O(1)。

回答by Nikopol

Typical map functions are also supported by the hashmap implementation in libmba.

libmba 中的 hashmap 实现也支持典型的映射函数。

I found about it a while ago, though I haven't used it. You can also check the library project homepage.

很久以前就发现了,虽然没用过。您还可以查看库项目主页

I hope it might be prove useful to someone.

我希望它可能对某人有用。