队列是一种向最后添加条目,从最前删除条目的数据结构,这种数据结构在处理按顺序到达的数据是很有用。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 ;
}
编译程序
[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,队列,glibc,tail,pop,queue,second,printf,GQueue From: https://www.cnblogs.com/opensmarty/p/17979446