glib库队列GQueue介绍
来源:互联网 发布:淘宝山东领航者怎么样 编辑:程序博客网 时间:2024/06/05 02:34
come from:http://blog.csdn.net/plusboy/article/details/1492570
队列是一种向最后添加条目,从最前删除条目的数据结构,这种数据结构在处理按顺序到达的数据是很有用。glib库提供的队列GQueue是一个双端队列, 它的实现基础是双向链表,所以它支持在队列的两端进行添加和删除,也支持很多其它的操作,比如在队列中进行插入和删除,但是我不推荐使用这样的功能,因为 当你经常需要在队列中进行插入和删除的时候,链表或许是个更好的选择。
下面的代码演示利用glib库中的GQueue向队头、队尾添加数据和从队头、队尾删除数据的操作。
编译程序
[plusboy@localhost c]$ gcc -o g_queue g_queue.c `pkg-config --cflags --libs glib-2.0`
程序执行结果
/**********************************************************
[plus@localhost c]$ ./g_queue
The queue is empty? YES
After push first, second and third at head of queue:
third second first
After push one, two and three at tail of queue:
third second first one two three
pop tail of queue: three
After pop tail:
third second first one two
pop head of queue: third
After pop head:
second first one two
pop 2nd of queue: first
After pop second:
second one two
After remove second:
one two
After insert 10 before index 1 and 20 after index 1:
one 10 two 20
***********************************************************/
说明:
1、向队列里添加和从队列里删除条目不返回任何值,所以为了再次使用队列要保存g_queue_new()返回的队列指针。
2、在队列的两端、中间都可以插入和删除条目,这就像排队的时候有人要插队,有人没排到就走了。
3、g_queue_peek_*函数可以取出队列中条目的内容进行检查,而不用从队列中删除该条目
队列是一种向最后添加条目,从最前删除条目的数据结构,这种数据结构在处理按顺序到达的数据是很有用。glib库提供的队列GQueue是一个双端队列, 它的实现基础是双向链表,所以它支持在队列的两端进行添加和删除,也支持很多其它的操作,比如在队列中进行插入和删除,但是我不推荐使用这样的功能,因为 当你经常需要在队列中进行插入和删除的时候,链表或许是个更好的选择。
下面的代码演示利用glib库中的GQueue向队头、队尾添加数据和从队头、队尾删除数据的操作。
/***************************************************************************
file: g_queue.c
desc: 这个文件用于演示glib库中队列的基本操作
compile: gcc -o g_queue g_queue.c `pkg-config --cflags --libs glib-2.0`
***************************************************************************/
#include <glib.h>
void display_queue(GQueue *queue, const char *prompt)
{
int len = g_queue_get_length(queue);
int i = 0;
printf("%s: /n", prompt);
for (i = 0; i < len; i++) {
printf("%s", g_queue_peek_nth(queue, i));
}
printf("/n");
}
int main(int argc, char *argv[])
{
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_head(queue, "third ");
display_queue(queue, "After push first, second and third at head of queue");
g_queue_push_tail(queue, "one ");
g_queue_push_tail(queue, "two ");
g_queue_push_tail(queue, "three ");
printf("After push one, two and three at tail of queue:/n");
g_queue_foreach(queue, (GFunc)printf, NULL);
printf("/n");
printf("pop tail of queue: %s", g_queue_pop_tail(queue));
display_queue(queue, "After pop tail");
printf("pop head of queue: %s /n", g_queue_pop_head(queue));
display_queue(queue, "After pop head");
/* queue index start from 0 */
printf("pop 2nd of queue: %s /n", g_queue_pop_nth(queue, 1));
display_queue(queue, "After pop second");
g_queue_remove(queue, "second ");
display_queue(queue, "After remove 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 ");
display_queue(queue, "After insert 10 before index 1 and 20 after index 1");
g_queue_free(queue);
return 0;
}
file: g_queue.c
desc: 这个文件用于演示glib库中队列的基本操作
compile: gcc -o g_queue g_queue.c `pkg-config --cflags --libs glib-2.0`
***************************************************************************/
#include <glib.h>
void display_queue(GQueue *queue, const char *prompt)
{
int len = g_queue_get_length(queue);
int i = 0;
printf("%s: /n", prompt);
for (i = 0; i < len; i++) {
printf("%s", g_queue_peek_nth(queue, i));
}
printf("/n");
}
int main(int argc, char *argv[])
{
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_head(queue, "third ");
display_queue(queue, "After push first, second and third at head of queue");
g_queue_push_tail(queue, "one ");
g_queue_push_tail(queue, "two ");
g_queue_push_tail(queue, "three ");
printf("After push one, two and three at tail of queue:/n");
g_queue_foreach(queue, (GFunc)printf, NULL);
printf("/n");
printf("pop tail of queue: %s", g_queue_pop_tail(queue));
display_queue(queue, "After pop tail");
printf("pop head of queue: %s /n", g_queue_pop_head(queue));
display_queue(queue, "After pop head");
/* queue index start from 0 */
printf("pop 2nd of queue: %s /n", g_queue_pop_nth(queue, 1));
display_queue(queue, "After pop second");
g_queue_remove(queue, "second ");
display_queue(queue, "After remove 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 ");
display_queue(queue, "After insert 10 before index 1 and 20 after index 1");
g_queue_free(queue);
return 0;
}
编译程序
[plusboy@localhost c]$ gcc -o g_queue g_queue.c `pkg-config --cflags --libs glib-2.0`
程序执行结果
/**********************************************************
[plus@localhost c]$ ./g_queue
The queue is empty? YES
After push first, second and third at head of queue:
third second first
After push one, two and three at tail of queue:
third second first one two three
pop tail of queue: three
After pop tail:
third second first one two
pop head of queue: third
After pop head:
second first one two
pop 2nd of queue: first
After pop second:
second one two
After remove second:
one two
After insert 10 before index 1 and 20 after index 1:
one 10 two 20
***********************************************************/
说明:
1、向队列里添加和从队列里删除条目不返回任何值,所以为了再次使用队列要保存g_queue_new()返回的队列指针。
2、在队列的两端、中间都可以插入和删除条目,这就像排队的时候有人要插队,有人没排到就走了。
3、g_queue_peek_*函数可以取出队列中条目的内容进行检查,而不用从队列中删除该条目
- glib库队列GQueue介绍
- glib库队列GQueue介绍
- glib中的GQueue结构体
- glib库介绍
- glib库的介绍
- glib库数组GArray介绍
- glib库中的GQuark介绍
- glib库数组GArray介绍
- GLib介绍
- glib介绍
- glib 介绍
- GLib介绍
- glib介绍
- Glib介绍
- glib库单向链表GSList介绍
- glib库双向链表GList介绍
- glib库hash表GHashTable介绍
- glib库数组GArray介绍(转)
- eVC编译后自动连接到目标板的取消方法
- Charm Bracelet
- libxml2解析xml到txt的实例
- 变量定义一个奇怪的问题,求解释
- 狐狸和山羊
- glib库队列GQueue介绍
- 整型转换成字符串
- glib库数组GArray介绍
- glib库单向链表GSList介绍
- 一个makefile文件的解析
- 异常:需求:”老师用电脑讲课“
- glib库双向链表GList介绍
- 人生12个经典领悟
- Kinect sdk与OpenNI的前世今生