网上的例子大多只是基于字符串的链表结合使用方法,特提供一个基于结构体的使用,同时对不常用的 自定义排序
g_list_sort , 自定义查找
g_list_find_custom使用。
以下是实例代码:
-
/*
-
* file: g_list.c
-
* desc: 这个文件用于演示glib库里双向链表GList的用法
-
* compile: gcc -o g_list g_list.c `pkg-config --cflags --libs glib-2.0`
-
*/
-
-
#include <glib.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
typedef struct stu{
-
long id;
-
int age;
-
char *name;
-
}person_t;
-
-
-
/* 构建一个节点 */
-
person_t * get_person_node( int id, int age, char *name)
-
{
-
person_t *pstu = (person_t *)calloc( sizeof(person_t), 1 );
-
if ( pstu == NULL ) return NULL;
-
pstu->id = id;
-
pstu->age = age;
-
pstu->name = (char *)strdup( (const char*)name );
-
-
return pstu;
-
}
-
-
/*遍历整个链表,打印显示*/
-
void display_list(GList *list)
-
{
-
GList *it = NULL;
-
person_t *pstu = NULL;
-
-
/* 遍历链表 */
-
for (it = list; it; it = it->next) {
-
pstu = it->data;
-
printf( "id=%d, age=%d, name=%s\r\n", pstu->id, pstu->age, pstu->name );
-
}
-
-
printf("\n");
-
}
-
-
/*删除一个节点*/
-
GList *list_remove_by_name(GList *list, char *name )
-
{
-
GList *it = NULL;
-
person_t *pstu = NULL;
-
-
/* 遍历链表 */
-
for (it = list; it; it = it->next) {
-
pstu = it->data;
-
if ( memcmp(pstu->name, name, strlen(name)) == 0)
-
{
-
//fprintf(stdout, "delete node:%s\r\n", pstu->name );
-
free( pstu->name );
-
free( pstu );
-
//GList *ret_list = g_list_remove(list,it);
-
GList *ret_list = g_list_remove_link(list,it);
-
}
-
}
-
return list;
-
}
-
-
/*释放链表,及其结点*/
-
void free_list_data(GList *list)
-
{
-
GList *it = NULL;
-
person_t *pstu = NULL;
-
-
/* 遍历链表 */
-
for (it = list; it; it = it->next) {
-
pstu = it->data;
-
free( pstu->name );
-
}
-
-
g_list_free(list);
-
-
printf("free list success \n");
-
}
-
-
/*按照名称比较*/
-
int my_comparator_by_name(gconstpointer item1, gconstpointer item2) {
-
person_t *psrc = (person_t *)(item1);
-
person_t *pdst = (person_t *)(item2);
-
//fprintf(stdout, "src:%s,dst:%s\r\n", psrc->name, pdst->name );
-
if ( memcmp(psrc->name,pdst->name,strlen(pdst->name) ) == 0 )
-
return 0;
-
else
-
return -1;
-
}
-
-
/*按照年龄比较*/
-
int my_comparator_by_age(gconstpointer item1, gconstpointer item2) {
-
person_t *psrc = (person_t *)(item1);
-
person_t *pdst = (person_t *)(item2);
-
if ( psrc->age > pdst->age )
-
return 1;
-
else if ( psrc->age < pdst->age )
-
return -1;
-
else
-
return 0;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
GList *list = NULL;
-
-
/* 向链表尾部追加节点 */
-
list = g_list_append(list, get_person_node(1,18,"hby") );
-
list = g_list_append(list, get_person_node(2,42,"tli") );
-
list = g_list_append(list, get_person_node(3,39,"gl") );
-
fprintf(stdout, "初始化链表输出.......\r\n");
-
display_list(list);
-
-
/*指定位置插入节点*/
-
list = g_list_insert(list, get_person_node(0,89,"sf"), 0 );
-
/*尾部追加*/
-
list = g_list_append(list, get_person_node(4,76,"fsd") );
-
fprintf(stdout, "插入更新后的链表输出.......\r\n");
-
display_list(list);
-
-
/*删除指定结点*/
-
list = list_remove_by_name(list, "tli");
-
list = list_remove_by_name(list, "hby");
-
fprintf(stdout, "删除结点后输出 [tli, hby].......\r\n");
-
display_list(list);
-
-
-
fprintf(stdout, "反转方式输出链表.......\r\n");
-
/* 从最后一个节点开始遍历链表 */
-
GList *it = NULL;
-
person_t *pstu = NULL;
-
it = g_list_last(list);
-
for (; it; it = it->prev) {
-
pstu = it->data;
-
fprintf( stdout, "id=%d, age=%d, name=%s\r\n", pstu->id, pstu->age, pstu->name );
-
}
-
fprintf(stdout, "\r\n");
-
-
/*自定义函数排序*/
-
list = g_list_insert(list, get_person_node(8,32,"skds"), 0 );
-
list = g_list_insert(list, get_person_node(6,45,"ff"), 0 );
-
list = g_list_sort(list, my_comparator_by_age);
-
fprintf(stdout, "自定义排序(按照年龄大小)后输出.......\r\n");
-
display_list(list);
-
-
/*自定义函数查找*/
-
fprintf(stdout, "自定义函数(按照姓名)查找.......\r\n");
-
person_t sfind;
-
memset(&sfind, 0x00, sizeof(person_t) );
-
sfind.name = g_strdup("fsd");
-
GList *item = g_list_find_custom(list,&sfind, my_comparator_by_name);
-
if ( item )
-
{ // 查找到
-
person_t *pdata = item->data;
-
fprintf(stdout, "find name[%s] age is:%d, id is:%d\r\n",
-
pdata->name, pdata->age, pdata->id );
-
}
-
else
-
fprintf(stdout, "find name[%s] NULL\r\n", sfind.name );
-
free(sfind.name);
-
fprintf(stdout, "\r\n");
-
-
-
/* 销毁链表 */
-
fprintf(stdout, "释放链表及结点内存.......\r\n");
-
free_list_data(list);
-
-
return 0;
-
}
===============================================================
编译:
#gcc -o g_list g_list.c `pkg-config --cflags --libs glib-2.0`
运行输出:
10122-hby-2:/home/hby/work/test/glibc # ./g_list
初始化链表输出.......
id=1, age=18, name=hby
id=2, age=42, name=tli
id=3, age=39, name=gl
插入更新后的链表输出.......
id=0, age=89, name=sf
id=1, age=18, name=hby
id=2, age=42, name=tli
id=3, age=39, name=gl
id=4, age=76, name=fsd
删除结点后输出 [tli, hby].......
id=0, age=89, name=sf
id=3, age=39, name=gl
id=4, age=76, name=fsd
反转方式输出链表.......
id=4, age=76, name=fsd
id=3, age=39, name=gl
id=0, age=89, name=sf
自定义排序(按照年龄大小)后输出.......
id=8, age=32, name=skds
id=3, age=39, name=gl
id=6, age=45, name=ff
id=4, age=76, name=fsd
id=0, age=89, name=sf
自定义函数(按照姓名)查找.......
find name[fsd] age is:76, id is:4
释放链表及结点内存.......
free list success
阅读(7816) | 评论(0) | 转发(1) |