1 .\" peter memishian -- meem@gnu.ai.mit.edu
2 .\" $Id: insque.3,v 1.2 1996/10/30 21:03:39 meem Exp meem $
3 .\" and Copyright (c) 2010, Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" References consulted:
8 .\" Linux libc source code (5.4.7)
9 .\" Solaris 2.x, OSF/1, and HP-UX manpages
10 .\" Curry's "UNIX Systems Programming for SVR4" (O'Reilly & Associates 1996)
12 .\" Changed to POSIX, 2003-08-11, aeb+wh
13 .\" mtk, 2010-09-09: Noted glibc 2.4 bug, added info on circular
14 .\" lists, added example program
16 .TH insque 3 (date) "Linux man-pages (unreleased)"
18 insque, remque \- insert/remove an item from a queue
21 .RI ( libc ", " \-lc )
24 .B #include <search.h>
26 .BI "void insque(void *" elem ", void *" prev );
27 .BI "void remque(void *" elem );
31 Feature Test Macro Requirements for glibc (see
32 .BR feature_test_macros (7)):
39 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
40 || /* Glibc >= 2.19: */ _DEFAULT_SOURCE
41 || /* Glibc <= 2.19: */ _SVID_SOURCE
48 functions manipulate doubly linked lists.
49 Each element in the list is a structure of
50 which the first two elements are a forward and a
52 The linked list may be linear (i.e., NULL forward pointer at
53 the end of the list and NULL backward pointer at the start of the list)
58 function inserts the element pointed to by \fIelem\fP
59 immediately after the element pointed to by \fIprev\fP.
61 If the list is linear, then the call
62 .I "insque(elem, NULL)"
63 can be used to insert the initial list element,
64 and the call sets the forward and backward pointers of
68 If the list is circular,
69 the caller should ensure that the forward and backward pointers of the
70 first element are initialized to point to that element,
75 call should also point to the element.
79 function removes the element pointed to by \fIelem\fP from the
82 For an explanation of the terms used in this section, see
90 Interface Attribute Value
94 T} Thread safety MT-Safe
100 POSIX.1-2001, POSIX.1-2008.
103 .\" e.g., SunOS, Linux libc4 and libc5
104 the arguments of these functions were of type \fIstruct qelem *\fP,
110 struct qelem *q_forw;
111 struct qelem *q_back;
117 This is still what you will get if
120 including \fI<search.h>\fP.
122 The location of the prototypes for these functions differs among several
124 The above is the POSIX version.
125 Some systems place them in \fI<string.h>\fP.
126 .\" Linux libc4 and libc 5 placed them
127 .\" in \fI<stdlib.h>\fP.
129 In glibc 2.4 and earlier, it was not possible to specify
132 Consequently, to build a linear list, the caller had to build a list
133 using an initial call that contained the first two elements of the list,
134 with the forward and backward pointers in each element suitably initialized.
136 The program below demonstrates the use of
138 Here is an example run of the program:
142 .RB "$ " "./a.out \-c a b c"
143 Traversing completed list:
147 That was a circular list
152 .\" SRC BEGIN (insque.c)
160 struct element *forward;
161 struct element *backward;
165 static struct element *
170 e = malloc(sizeof(*e));
172 fprintf(stderr, "malloc() failed\en");
180 main(int argc, char *argv[])
182 struct element *first, *elem, *prev;
183 int circular, opt, errfnd;
185 /* The "\-c" command\-line option can be used to specify that the
190 while ((opt = getopt(argc, argv, "c")) != \-1) {
201 if (errfnd || optind >= argc) {
202 fprintf(stderr, "Usage: %s [\-c] string...\en", argv[0]);
206 /* Create first element and place it in the linked list. */
208 elem = new_element();
211 elem\->name = argv[optind];
214 elem\->forward = elem;
215 elem\->backward = elem;
221 /* Add remaining command\-line arguments as list elements. */
223 while (++optind < argc) {
226 elem = new_element();
227 elem\->name = argv[optind];
231 /* Traverse the list from the start, printing element names. */
233 printf("Traversing completed list:\en");
236 printf(" %s\en", elem\->name);
237 elem = elem\->forward;
238 } while (elem != NULL && elem != first);
241 printf("That was a circular list\en");