首页 > 其他分享 >【glibc】glib库队列GQueue介绍

【glibc】glib库队列GQueue介绍

时间:2024-01-22 10:33:53浏览次数:20  
标签:glib 队列 glibc tail pop queue second printf GQueue

队列是一种向最后添加条目,从最前删除条目的数据结构,这种数据结构在处理按顺序到达的数据是很有用。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

相关文章

  • spring--CGLIB动态代理的实现原理
    CGLIB(CodeGenerationLibrary)是一个强大的、高性能、高质量的代码生成库,它可以在运行时扩展Java类和实现Java接口。CGLIB动态代理是基于继承的方式来实现的,它不需要接口,可以代理普通类。以下是CGLIB动态代理的实现原理:继承:CGLIB动态代理通过继承目标类来创建子类,并在......
  • spring--JDK动态代理和CGLIB代理的区别
    JDK动态代理和CGLIB代理是Java中常用的两种动态代理实现方式,它们各有特点和适用场景:JDK动态代理:JDK动态代理是基于接口的代理方式,它使用Java反射机制来创建代理对象,并要求目标对象实现一个或多个接口。在代理过程中,JDK动态代理会创建一个实现了目标对象所有接口的代......
  • 记录一下 ArrayBlockingQueue 消息堆积的问题
    前言由于之前这个系统的日志记录是被领导要求写表的,在不影响系统性能的前提下,日志的入库操作肯定是要改成异步进行的,当时利用ArrayBlockingQueue+线程+AOP简单的去实现了一下,但是初版代码测试下来发现了一个很严重的问题,就是日志丢失的问题,本文由此而来。初步构思代码实现逻辑实......
  • /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found问题解决
    有一个go实现的项目代码最近有更新,自己在开发环境上手动构建并运行都没有问题(构建和运行时相同环境,肯定没有问题^_^)。后面通过jenkins构建镜像也没有问题,运行时却报错 之前的版本在jenkins上构建也是成功的,后沟通得知jenkins集群版本最近有更新 那么,大概知道原因了,由于jenk......
  • GLIBC修复笔记
    微信公众号:运维开发故事作者:wanger背景公司环境有台机器硬盘故障,需要安装megacli工具进系统查看raid信息,在ubuntu20.04安装megacli解决依赖过程中失误将高版本的libtinfo包装系统上了,导致系统报glibc对应的版本没有找到,系统实际glibc的版本是2.31。解决办法如果系统的ssh没有断开,可......
  • 解决/lib64/libc.so.6: version `GLIBC_2.14' not found(Linux环境)
    ......
  • PriorityBlockingQueue 优先级队列
    packagestudy;importlombok.Data;importjava.util.Comparator;importjava.util.concurrent.PriorityBlockingQueue;publicclassPriorityBlockingQueueDemo{publicstaticvoidmain(String[]args)throwsInterruptedException{PriorityBlocki......
  • 阻塞队列之 LinkedBlockingQueue
    LinkedBlockingQueue:Java多线程编程中的阻塞队列在Java多线程编程中,LinkedBlockingQueue是一个非常重要的类,它提供了一种用于在生产者和消费者之间进行数据传递的机制。LinkedBlockingQueue广泛应用于各种场景,如线程池、任务队列等。本文将详细介绍LinkedBlockingQueue的原理......
  • 高版本gcc编译出的程序在低版本glibc机器上运行
    比如我们用gcc9.3.0编译程序,但需要发布的机器gcc版本是4.8.5,怎么办?你可能想到如下方法静态编译容器发布打包依赖的so,使用本地so运行程序1.静态编译将libc和libstdc++静态编译,编译时带上如下参数。g++-static-libgcc-static-libstdc++glibc并不推荐静态链接,你依赖......
  • 在idea中无法查看org.springframework.cglib.core.Signature.java 源码
    一、现象1.点击ideaDownloadSource,没有变化;ChouseSource也无济于事。2.打开下载的源码包,惊奇的发现确实没有对应的源码。3.问题发生在哪儿?原来是这些包原本不属于spring,spring在编译的时候直接修改了包名称。 ......