技术积累1( C技巧/GLib数据结构 ) LeoWL的专栏 博客频道 CSDN.NET

技术积累-1( C技巧/GLib数据结构 ) - LeoWL的专栏 - 博客频道 - CSDN.NET

技术积累-1( C技巧/GLib数据结构 )


分类:
Study


281人阅读
评论(0)
收藏
举报

// 我承认,下面的内容很大部分是我从网络上找来的

1、C 语言中动态创建二维数组

--------------------------

题目要求输入m和n,然后再定义二维数组a[m][n]

可以这样做:(以int型为例)

int **a;

int m,n,i;

scanf("%d%d",&m,&n);

/* malloc函数在stdlib.h里面,用的时候加入这个头文件 */

a=(int**)malloc(m*sizeof(int*));

for(i=0;i<m;i++)

a[i]=(int*)malloc(n*sizeof(int));

/* 这样以后你就可以把a当作二维数组a[m][n]来用了。。 */

/* ========================================================================== */

2、glib库数据结构

-----------------

编译:gcc -o g_array g_array.c `pkg-config --cflags --libs glib-2.0`

glib库里实现了一些基本的数据结构,比如单向链表,双向链表、队列、树、Hash表、数组。

2.1) glib库单向链表 GSList

typedef struct {

  gpointer data;

  GSList  *next;

} GSList;

data 成员定义为gpointer(即void*),可以放任何类型的数据。

next 指向下一个结点

2.2) glib库双向链表 GList

typedef struct {

  gpointer data;

  GList   *next;

  GList   *prev;

} GList;

prev 指向上一个结点

CODE:

/* 创建 */

GList *list = NULL;

/* 向链表尾部追加节点 */

    list = g_list_append(list, "one ");

    list = g_list_append(list, "two ");

    list = g_list_append(list, "three ");

    list = g_list_append(list, "four ");

    list = g_list_append(list, "five ");

/* 在链表头部插入 */

    list = g_list_prepend(list, "zero ");

/* 查找链表中的节点 */

    GList *it = NULL;

    it = g_list_find(list, "two ");

    printf("Find data "two ": %s/n", it->data);

/* 确定链表指针指向链表中的第几个节点 */

    int index = 0;

    index = g_list_position(list, it);

    printf("index of "two " is: %d/n", index);

    it = g_list_nth(list, 1);

    printf("%s/n", it->data);

/* 从链表里删除一个节点 */

    list = g_list_remove(list, "three ");

/* 向链表里插入一个节点 */

    list = g_list_insert(list, "INSERT ", 3);

/* 采用内循环遍历链表节点 */

    g_list_foreach(list, (GFunc)printf, NULL);

/* 取链表的最后一个节点 */

    GList *last = NULL;

    last = g_list_last(list);

/* 从最后一个节点开始遍历链表 */

    for (it = last; it; it = it->prev)

        printf("%s", it->data);

/* 销毁链表 */

    g_list_free(list);

自定义内循环处理函数:

void print_data(char* data) {

    count++;

    printf("count %d/n data is %s/n",count,data);

}

g_list_foreach(list,print_data,list->data);

2.3) glib库队列 GQueue

typedef struct {

  GList *head;

  GList *tail;

  guint  length;

} GQueue;

head 指向队列的第一个元素

tail 指向队列的最后一个元素

length 队列中元素的个数

CODE:

GQueue *queue = NULL;

queue = g_queue_new();

printf("The queue is empty? %s ", g_queue_is_empty(queue) ? "YES" : "NO");

    g_queue_push_head(queue, "first ");

    g_queue_push_head(queue, "second ");

    g_queue_push_tail(queue, "one ");

    g_queue_push_tail(queue, "two ");

    g_queue_foreach(queue, (GFunc)printf, NULL);

    printf("pop tail of queue: %s ", g_queue_pop_tail(queue));

    printf("pop head of queue: %s /n", g_queue_pop_head(queue));

    /* queue index start from 0 */

    printf("pop 2nd of queue: %s /n", g_queue_pop_nth(queue, 1));

    g_queue_remove(queue, "second ");

    GList *list = NULL;

    list = g_queue_peek_nth_link(queue, 1);

    g_queue_insert_before(queue, list, "10 ");

    g_queue_insert_after(queue, list, "20 ");

    g_queue_free(queue);

说明:

1、向队列里添加和从队列里删除条目不返回任何值,所以为了再次使用队列要保存

    g_queue_new()返回的队列指针。

2、在队列的两端、中间都可以插入和删除条目,这就像排队的时候有人要插队,有人没排到就走了。

3、g_queue_peek_*函数可以取出队列中条目的内容进行检查,而不用从队列中删除该条目。

2.4) glib库数组 GArray

glib库中的数组GArray类型很像C++标准容器库中的vector容器。要使用glib库中的数组中

需要声明一个指向GArray类型的指针。

typedef struct {

  gchar *data;

  guint len;

} GArray;

原文地址:https://www.cnblogs.com/lexus/p/2551668.html