322 lines
10 KiB
HTML
322 lines
10 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of INSQUE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>INSQUE</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
insque, remque - insert/remove an item from a queue
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/search.h">search.h</A>></B>
|
|
|
|
<B>void insque(void *</B><I>elem</I><B>, void *</B><I>prev</I><B>);</B>
|
|
|
|
<B>void remque(void *</B><I>elem</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
|
|
<B>insque</B>(),
|
|
|
|
<B>remque</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
_XOPEN_SOURCE >= 500
|
|
|
|
<BR> || /* Glibc since 2.19: */ _DEFAULT_SOURCE
|
|
<BR> || /* Glibc versions <= 2.19: */ _SVID_SOURCE
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>insque</B>()
|
|
|
|
and
|
|
<B>remque</B>()
|
|
|
|
functions manipulate doubly-linked lists.
|
|
Each element in the list is a structure of
|
|
which the first two elements are a forward and a
|
|
backward pointer.
|
|
The linked list may be linear (i.e., NULL forward pointer at
|
|
the end of the list and NULL backward pointer at the start of the list)
|
|
or circular.
|
|
<P>
|
|
|
|
The
|
|
<B>insque</B>()
|
|
|
|
function inserts the element pointed to by <I>elem</I>
|
|
immediately after the element pointed to by <I>prev</I>.
|
|
<P>
|
|
|
|
If the list is linear, then the call
|
|
<I>insque(elem, NULL)</I>
|
|
|
|
can be used to insert the initial list element,
|
|
and the call sets the forward and backward pointers of
|
|
<I>elem</I>
|
|
|
|
to NULL.
|
|
<P>
|
|
|
|
If the list is circular,
|
|
the caller should ensure that the forward and backward pointers of the
|
|
first element are initialized to point to that element,
|
|
and the
|
|
<I>prev</I>
|
|
|
|
argument of the
|
|
<B>insque</B>()
|
|
|
|
call should also point to the element.
|
|
<P>
|
|
|
|
The
|
|
<B>remque</B>()
|
|
|
|
function removes the element pointed to by <I>elem</I> from the
|
|
doubly-linked list.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>insque</B>(),
|
|
|
|
<B>remque</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAF"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
On ancient systems,
|
|
|
|
the arguments of these functions were of type <I>struct qelem *</I>,
|
|
defined as:
|
|
<P>
|
|
|
|
|
|
|
|
struct qelem {
|
|
<BR> struct qelem *q_forw;
|
|
<BR> struct qelem *q_back;
|
|
<BR> char q_data[1];
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
This is still what you will get if
|
|
<B>_GNU_SOURCE</B>
|
|
|
|
is defined before
|
|
including <I><<A HREF="file:///usr/include/search.h">search.h</A>></I>.
|
|
<P>
|
|
|
|
The location of the prototypes for these functions differs among several
|
|
versions of UNIX.
|
|
The above is the POSIX version.
|
|
Some systems place them in <I><<A HREF="file:///usr/include/string.h">string.h</A>></I>.
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
In glibc 2.4 and earlier, it was not possible to specify
|
|
<I>prev</I>
|
|
|
|
as NULL.
|
|
Consequently, to build a linear list, the caller had to build a list
|
|
using an initial call that contained the first two elements of the list,
|
|
with the forward and backward pointers in each element suitably initialized.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>insque</B>().
|
|
|
|
Here is an example run of the program:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>./a.out -c a b c</B>
|
|
|
|
Traversing completed list:
|
|
<BR> a
|
|
<BR> b
|
|
<BR> c
|
|
That was a circular list
|
|
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/search.h">search.h</A>>
|
|
<P>
|
|
struct element {
|
|
<BR> struct element *forward;
|
|
<BR> struct element *backward;
|
|
<BR> char *name;
|
|
};
|
|
<P>
|
|
static struct element *
|
|
new_element(void)
|
|
{
|
|
<BR> struct element *e;
|
|
<P>
|
|
<BR> e = malloc(sizeof(struct element));
|
|
<BR> if (e == NULL) {
|
|
<BR> fprintf(stderr, "malloc() failed\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> return e;
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct element *first, *elem, *prev;
|
|
<BR> int circular, opt, errfnd;
|
|
<P>
|
|
<BR> /* The "-c" command-line option can be used to specify that the
|
|
<BR> list is circular */
|
|
<P>
|
|
<BR> errfnd = 0;
|
|
<BR> circular = 0;
|
|
<BR> while ((opt = getopt(argc, argv, "c")) != -1) {
|
|
<BR> switch (opt) {
|
|
<BR> case 'c':
|
|
<BR> circular = 1;
|
|
<BR> break;
|
|
<BR> default:
|
|
<BR> errfnd = 1;
|
|
<BR> break;
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> if (errfnd || optind >= argc) {
|
|
<BR> fprintf(stderr, "Usage: %s [-c] string...\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Create first element and place it in the linked list */
|
|
<P>
|
|
<BR> elem = new_element();
|
|
<BR> first = elem;
|
|
<P>
|
|
<BR> elem->name = argv[optind];
|
|
<P>
|
|
<BR> if (circular) {
|
|
<BR> elem->forward = elem;
|
|
<BR> elem->backward = elem;
|
|
<BR> insque(elem, elem);
|
|
<BR> } else {
|
|
<BR> insque(elem, NULL);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Add remaining command-line arguments as list elements */
|
|
<P>
|
|
<BR> while (++optind < argc) {
|
|
<BR> prev = elem;
|
|
<P>
|
|
<BR> elem = new_element();
|
|
<BR> elem->name = argv[optind];
|
|
<BR> insque(elem, prev);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Traverse the list from the start, printing element names */
|
|
<P>
|
|
<BR> printf("Traversing completed list:\n");
|
|
<BR> elem = first;
|
|
<BR> do {
|
|
<BR> printf(" %s\n", elem->name);
|
|
<BR> elem = elem->forward;
|
|
<BR> } while (elem != NULL && elem != first);
|
|
<P>
|
|
<BR> if (elem == first)
|
|
<BR> printf("That was a circular list\n");
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+queue">queue</A></B>(3)
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="5"><A HREF="#lbAE">ATTRIBUTES</A><DD>
|
|
<DT id="6"><A HREF="#lbAF">CONFORMING TO</A><DD>
|
|
<DT id="7"><A HREF="#lbAG">NOTES</A><DD>
|
|
<DT id="8"><A HREF="#lbAH">BUGS</A><DD>
|
|
<DT id="9"><A HREF="#lbAI">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="10"><A HREF="#lbAJ">Program source</A><DD>
|
|
</DL>
|
|
<DT id="11"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="12"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:46 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|