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 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" References consulted:
28 .\" Linux libc source code (5.4.7)
29 .\" Solaris 2.x, OSF/1, and HP-UX manpages
30 .\" Curry's "UNIX Systems Programming for SVR4" (O'Reilly & Associates 1996)
32 .\" Changed to POSIX, 2003-08-11, aeb+wh
33 .\" mtk, 2010-09-09: Noted glibc 2.4 bug, added info on circular
34 .\" lists, added example program
36 .TH INSQUE 3 2017-09-15 "" "Linux Programmer's Manual"
38 insque, remque \- insert/remove an item from a queue
41 .B #include <search.h>
43 .BI "void insque(void *" elem ", void *" prev );
45 .BI "void remque(void *" elem );
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
57 _XOPEN_SOURCE\ >=\ 500
58 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
59 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
60 || /* Glibc versions <= 2.19: */ _SVID_SOURCE
68 functions manipulate doubly-linked lists.
69 Each element in the list is a structure of
70 which the first two elements are a forward and a
72 The linked list may be linear (i.e., NULL forward pointer at
73 the end of the list and NULL backward pointer at the start of the list)
78 function inserts the element pointed to by \fIelem\fP
79 immediately after the element pointed to by \fIprev\fP.
81 If the list is linear, then the call
82 .I "insque(elem, NULL)"
83 can be used to insert the initial list element,
84 and the call sets the forward and backward pointers of
88 If the list is circular,
89 the caller should ensure that the forward and backward pointers of the
90 first element are initialized to point to that element,
95 call should also point to the element.
99 function removes the element pointed to by \fIelem\fP from the
102 For an explanation of the terms used in this section, see
108 Interface Attribute Value
112 T} Thread safety MT-Safe
116 POSIX.1-2001, POSIX.1-2008.
119 .\" e.g., SunOS, Linux libc4 and libc5
120 the arguments of these functions were of type \fIstruct qelem *\fP,
126 struct qelem *q_forw;
127 struct qelem *q_back;
133 This is still what you will get if
136 including \fI<search.h>\fP.
138 The location of the prototypes for these functions differs among several
140 The above is the POSIX version.
141 Some systems place them in \fI<string.h>\fP.
142 .\" Linux libc4 and libc 5 placed them
143 .\" in \fI<stdlib.h>\fP.
145 In glibc 2.4 and earlier, it was not possible to specify
148 Consequently, to build a linear list, the caller had to build a list
149 using an initial call that contained the first two elements of the list,
150 with the forward and backward pointers in each element suitably initialized.
152 The program below demonstrates the use of
154 Here is an example run of the program:
158 .RB "$ " "./a.out -c a b c"
159 Traversing completed list:
163 That was a circular list
175 struct element *forward;
176 struct element *backward;
180 static struct element *
185 e = malloc(sizeof(struct element));
187 fprintf(stderr, "malloc() failed\\n");
195 main(int argc, char *argv[])
197 struct element *first, *elem, *prev;
198 int circular, opt, errfnd;
200 /* The "\-c" command\-line option can be used to specify that the
205 while ((opt = getopt(argc, argv, "c")) != \-1) {
216 if (errfnd || optind >= argc) {
217 fprintf(stderr, "Usage: %s [\-c] string...\\n", argv[0]);
221 /* Create first element and place it in the linked list */
223 elem = new_element();
226 elem\->name = argv[optind];
229 elem\->forward = elem;
230 elem\->backward = elem;
236 /* Add remaining command\-line arguments as list elements */
238 while (++optind < argc) {
241 elem = new_element();
242 elem\->name = argv[optind];
246 /* Traverse the list from the start, printing element names */
248 printf("Traversing completed list:\\n");
251 printf(" %s\\n", elem\->name);
252 elem = elem\->forward;
253 } while (elem != NULL && elem != first);
256 printf("That was a circular list\\n");