语法

#include <search.h>

void insque(void *elem, void *prev);

void remque(void *elem);

glibc的功能测试宏要求(请参阅feature_test_macros(7)):

insque(),remque():

_XOPEN_SOURCE>= 500 || / * Glibc自2.19开始:* / -_DEFAULT_SOURCE || / * Glibc版本

属性

有关本节中使用的术语的说明,请参见attribute(7)。

InterfaceAttributeValue
insque(),remque()Thread safetyMT-Safe
INSQUE - Linux手册页

Linux程序员手册 第3部分
更新日期: 2020-06-09

另外参见

队列(3)

出版信息

这个页面是Linux手册页项目5.08版的一部分。有关项目的说明、有关报告错误的信息以及此页面的最新版本,请访问https://www.kernel.org/doc/man-pages/

备注

在古代系统上,这些函数的参数为​​struct qelem *类型,定义为:

struct qelem {
    struct qelem *q_forw;
    struct qelem *q_back;
    char          q_data[1];
};

如果在包括之前定义了_GNU_SOURCE,仍然会得到此结果。

这些功能的原型的位置在UNIX的多个版本中有所不同。以上是POSIX版本。有些系统将它们放入。

名称

insque,remque-从队列中插入/删除项目

示例

下面的程序演示了insque()的用法。这是该程序的示例运行:

$ ./a.out -c a b c
Traversing completed list:
    a
    b
    c
That was a circular list

Program source

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <search.h>

struct element {
    struct element *forward;
    struct element *backward;
    char *name;
};

static struct element *
new_element(void)
{
    struct element *e;

    e = malloc(sizeof(struct element));
    if (e == NULL) {
        fprintf(stderr, "malloc() failed\n");
        exit(EXIT_FAILURE);
    }

    return e;
}

int
main(int argc, char *argv[])
{
    struct element *first, *elem, *prev;
    int circular, opt, errfnd;

    /* The "-c" command-line option can be used to specify that the
       list is circular */

    errfnd = 0;
    circular = 0;
    while ((opt = getopt(argc, argv, "c")) != -1) {
        switch (opt) {
        case 'c':
            circular = 1;
            break;
        default:
            errfnd = 1;
            break;
        }
    }

    if (errfnd || optind >= argc) {
        fprintf(stderr,  "Usage: %s [-c] string...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Create first element and place it in the linked list */

    elem = new_element();
    first = elem;

    elem->name = argv[optind];

    if (circular) {
        elem->forward = elem;
        elem->backward = elem;
        insque(elem, elem);
    } else {
        insque(elem, NULL);
    }

    /* Add remaining command-line arguments as list elements */

    while (++optind < argc) {
        prev = elem;

        elem = new_element();
        elem->name = argv[optind];
        insque(elem, prev);
    }

    /* Traverse the list from the start, printing element names */

    printf("Traversing completed list:\n");
    elem = first;
    do {
        printf("    %s\n", elem->name);
        elem = elem->forward;
    } while (elem != NULL && elem != first);

    if (elem == first)
        printf("That was a circular list\n");

    exit(EXIT_SUCCESS);
}

说明

insque()和remque()函数可处理双向链接列表。列表中的每个元素都是前两个指针为前向和后向指针的结构。链接列表可以是线性的(即,列表末尾为NULL前向指针,列表开头为NULL后向指针)或圆形。

insque()函数在prev指向的元素之后立即插入elem指向的元素。

如果列表是线性的,则可以使用insque(elem,NULL)调用来插入初始列表元素,并且此调用将elem的前向和后向指针设置为NULL。

如果列表是循环的,则调用者应确保将第一个元素的前后指针初始化为指向该元素,并且insque()调用的prev参数也应指向该元素。

remque()函数从双向链接列表中删除elem指向的元素。

遵循规范

POSIX.1-2001,POSIX.1-2008。

BUGS

在glibc 2.4和更低版本中,无法将prev指定为NULL。因此,要构建线性列表,调用者必须使用包含列表的前两个元素的初始调用来构建列表,并适当初始化每个元素中的前向和后向指针。

日期:2019-08-20 18:01:10 来源:oir作者:oir