Coder Social home page Coder Social logo

hlibc's Introduction

hlibc

基于C语言的通用型数据结构与容器库

hlist

双向序列容器,用于将它们的元素保持为线性排列,并允许在序列的任何位置插入和删除。

struct create_node !!!对于堆数据来说,本库提供的clear函数不能释放开发者申请的堆空间,开发人员应该自己负责清理申请的堆空间。

API

创建和删除一个容器

/**
 * 创建一个容器
 * @param type_size 装入容器的数据类型的大小
 * @return 返回新创建的容器
 */
hlist_ptr_t hlist_create(uint32_t type_size);

/**
 * 删除给定的容器
 * @param list 一个由 hlist_create 返回的容器
 */
extern void hlist_destroy(hlist_ptr_t list);

/* 装入容器的数据类型为 int */
hlist_ptr_t list = hlist_create(sizeof(int));
hlist_destroy(list);

迭代器

hlist_iterator_ptr_t it = hlist_begin(list);
hlist_iter_forward(&it);
std::cout << "it = " << DATA_CAST(int)hlist_iter_data(it) << std::endl;
/* ... */
for (hlist_iterator_ptr_t it = hlist_begin(list); it != hlist_end(list); hlist_iter_forward(&it)) {
    std::cout << DATA_CAST(int)hlist_iter_data(it) << " ";
}

插入节点,两种方式插入一个节点:1、在头部或者尾部插入;2、使用迭代器插入

struct test_str
{
    char a;
    int b;
};
/* ... */
hlist_ptr_t list1 = hlist_create(sizeof(struct test_str*));
struct test_str* t1 = (struct test_str*)malloc(sizeof (struct test_str));
t1->a = 'a';
t1->b = 10;
hlist_push_back(list1, &t1, sizeof(t1));
struct test_str* t2 = (struct test_str*)malloc(sizeof (struct test_str));
t2->a = 'b';
t2->b = 20;
hlist_push_front(list1, &t2, sizeof(t2));

/* ... */
struct test_str t1;
hlist_ptr_t list2 = hlist_create(sizeof(struct test_str));
t1.a = 'a';
t1.b = 10;
hlist_iterator_ptr_t it = hlist_end(list2);
hlist_insert(list2, it, &t1, sizeof(t1));

删除元素

hlist_pop_back(list);
hlist_pop_front(list);

hstack

stack容器遵循 LIFO(后进先出)语义。 堆栈上最后推送的元素将第一个弹出。

API

创建和删除一个容器

/**
 * 创建一个 stack 容器
 * @param type_size 装入容器的数据类型的大小。例:`hstack_create(sizeof(int));`
 * @return 返回新创建的 stack 容器
 */
hstack_ptr_t hstack_create(uint32_t type_size);

/**
 * 删除给定的 stack 容器
 * @param stack 一个由 `hstack_create` 返回的容器
 */
void hstack_destroy(hstack_ptr_t stack);

/* ... */
hstack_ptr_t stack = hlist_create(sizeof(int));
hlist_destroy(stack);

hqueue

queue容器遵循 FIFO(先进先出)语义。 第一个推送(即插入队列中)的元素将第一个弹出(即从队列中删除)。

API

创建和删除一个容器

/**
 * 创建一个 queue 容器
 * @param type_size 装入容器的数据类型的大小。例:`hqueue_create(sizeof(int));`
 * @return 返回新创建的 queue 容器
 */
hqueue_ptr_t hqueue_create(uint32_t type_size);

/**
 * 删除给定的 queue 容器
 * @param queue 一个由 `hqueue_create` 返回的容器
 */
void hqueue_destroy(hqueue_ptr_t queue);

/* ... */
hstack_ptr_t queue = hlist_create(sizeof(int));
hlist_destroy(queue);

1、为什么要写这个库?
在C语言中,由于不能泛型编程,所以能开箱即用的数据结构很少,一般都要自己造轮子(现在亦是如此)。

2、如何使用C语言进行“泛型”编程?
虽然在C语言中不能使用模板,但是可以使用万能指针(void*)来替代。

3、目前有哪些使用方式?

hlibc's People

Contributors

hellototoro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.