glib 双向链表

原文地址: http://hi.baidu.com/study_together/blog/item/f14cb83319f70d94a8018e3e.html

编译: 编译:gcc -g -Wall -O0 fuck.c -o fuck `pkg-config --libs --cflags glib-2.0`

1

基本操作

这里是使用 GList 可以进行的一些常见操作:

#include <glib.h>
#include
<stdio.h>

int main(int argc, char** argv) {
GList
* list = NULL;
list
= g_list_append(list, "Austin ");
printf(
"The first item is '%s'\n", list->data);
list
= g_list_insert(list, "Baltimore ", 1);
printf(
"The second item is '%s'\n", g_list_next(list)->data);
list
= g_list_remove(list, "Baltimore ");
printf(
"After removal of 'Baltimore', the list length is %d\n", g_list_length(list));
GList
* other_list = g_list_append(NULL, "Baltimore ");
list
= g_list_concat(list, other_list);
printf(
"After concatenation: ");
g_list_foreach(list, (GFunc)printf, NULL);
list
= g_list_reverse(list);
printf(
"\nAfter reversal: ");
g_list_foreach(list, (GFunc)printf, NULL);
g_list_free(list);
return 0;
}

***** Output *****

The first item is 'Austin '
The second item is 'Baltimore '
After removal of 'Baltimore', the list length is 1
After concatenation: Austin Baltimore
After reversal: Baltimore Austin

2

更好的导航

已经了解了一些基本的 GList 操作后,这里是一些可能的操作,唯一的原因就是 GList 中的每个节点都有一个指向前一个节点的链接:

#include <glib.h>
#include
<stdio.h>

int main(int argc, char** argv) {
GList
* list = g_list_append(NULL, "Austin ");
list
= g_list_append(list, "Bowie ");
list
= g_list_append(list, "Charleston ");
printf(
"Here's the list: ");
g_list_foreach(list, (GFunc)printf, NULL);
GList
* last = g_list_last(list);
printf(
"\nThe first item (using g_list_first) is '%s'\n", g_list_first(last)->data);
printf(
"The next-to-last item is '%s'\n", g_list_previous(last)->data);
printf(
"The next-to-last item is '%s'\n", g_list_nth_prev(last, 1)->data);
g_list_free(list);
return 0;
}

***** Output *****

Here's the list: Austin Bowie Charleston
The first item (using g_list_first) is 'Austin '
The next-to-last item is 'Bowie '
The next-to-last item is 'Bowie '

3

使用链接删除节点

已经了解了在拥有指向其容纳的数据的指针的前提下如何从列表中删除一个节点;g_list_remove 可以很好地完成。

如果拥有一个指向节点本身的指针,可以通过一个快速的 O(1) 操作直接删除那个节点。

#include <glib.h>
#include
<stdio.h>

int main(int argc, char** argv) {
GList
* list = g_list_append(NULL, "Austin ");
list
= g_list_append(list, "Bowie ");
list
= g_list_append(list, "Chicago ");
printf(
"Here's the list: ");
g_list_foreach(list, (GFunc)printf, NULL);
GList
* bowie = g_list_nth(list, 1);
list
= g_list_remove_link(list, bowie);
g_list_free_1(bowie);
printf(
"\nHere's the list after the remove_link call: ");
g_list_foreach(list, (GFunc)printf, NULL);
list
= g_list_delete_link(list, g_list_nth(list, 1));
printf(
"\nHere's the list after the delete_link call: ");
g_list_foreach(list, (GFunc)printf, NULL);
g_list_free(list);
return 0;
}

***** Output *****

Here's the list: Austin Bowie Chicago
Here's the list after the remove_link call: Austin Chicago
Here's the list after the delete_link call: Austin


4

索引和位置

如果只是想找出某个条目在 GList 中的位置,那么有两种选择。可以使用 g_list_index,它可以使用条目中的数据找出它,

或者可以使用 g_list_position,它使用的是指向那个节点的指针。这个示例展示了这两种方法:

#include <glib.h>
#include
<stdio.h>

int main(int argc, char** argv) {
GList
* list = g_list_append(NULL, "Austin ");
list
= g_list_append(list, "Bowie ");
list
= g_list_append(list, "Bowie ");
list
= g_list_append(list, "Cheyenne ");
printf(
"Here's the list: ");
g_list_foreach(list, (GFunc)printf, NULL);
printf(
"\nItem 'Bowie' is located at index %d\n", g_list_index(list, "Bowie "));
printf(
"Item 'Dallas' is located at index %d\n", g_list_index(list, "Dallas"));
GList
* last = g_list_last(list);
printf(
"Item 'Cheyenne' is located at index %d\n", g_list_position(list, last));
g_list_free(list);
return 0;
}

***** Output *****

Here's the list: Austin Bowie Bowie Cheyenne
Item 'Bowie' is located at index 1
Item 'Dallas' is located at index -1
Item 'Cheyenne' is located at index 3

posted @ 2011-07-09 21:13  wangkangluo1  阅读(1092)  评论(0编辑  收藏  举报