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 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code (5.4.7)
27 .\" Solaris 2.x, OSF/1, and HP-UX manpages
28 .\" Curry's "UNIX Systems Programming for SVR4" (O'Reilly & Associates 1996)
30 .\" Changed to POSIX, 2003-08-11, aeb+wh
31 .\" mtk, 2010-09-09: Noted glibc 2.4 bug, added info on circular
32 .\" lists, added example program
34 .TH INSQUE 3 2010-09-09 "" "Linux Programmer's Manual"
36 insque, remque \- insert/remove an item from a queue
39 .B #include <search.h>
41 .BI "void insque(void *" elem ", void *" prev );
43 .BI "void remque(void *" elem );
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
55 _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
56 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
64 functions manipulate doubly-linked lists.
65 Each element in the list is a structure of
66 which the first two elements are a forward and a
68 The linked list may be linear (i.e., NULL forward pointer at
69 the end of the list and NULL backward pointer at the start of the list)
74 function inserts the element pointed to by \fIelem\fP
75 immediately after the element pointed to by \fIprev\fP.
77 If the list is linear, then the call
78 .I "insque(elem, NULL)"
79 can be used to insert the initial list element,
80 and the call sets the forward and backward pointers of
84 If the list is circular,
85 the caller should ensure that the forward and backward pointers of the
86 first element are initialized to point to that element,
91 call should also point to the element.
95 function removes the element pointed to by \fIelem\fP from the
100 Traditionally (e.g., SunOS, Linux libc 4 and libc 5),
101 the arguments of these functions were of type \fIstruct qelem *\fP,
107 struct qelem *q_forw;
108 struct qelem *q_back;
114 This is still what you will get if
117 including \fI<search.h>\fP.
119 The location of the prototypes for these functions differs among several
121 The above is the POSIX version.
122 Some systems place them in \fI<string.h>\fP.
123 Linux libc4 and libc 5 placed them
126 In glibc 2.4 and earlier, it was not possible to specify
129 Consequently, to build a linear list, the caller had to build a list
130 using an initial call that contained the first two elements of the list,
131 with the forward and backward pointers in each element suitably initialized.
133 The program below demonstrates the use of
135 Here is an example run of the program:
139 .RB "$ " "./a.out -c a b c"
140 Traversing completed list:
144 That was a circular list
156 struct element *forward;
157 struct element *backward;
161 static struct element *
166 e = malloc(sizeof(struct element));
168 fprintf(stderr, "malloc() failed\\n");
176 main(int argc, char *argv[])
178 struct element *first, *elem, *prev;
179 int circular, opt, errfnd;
181 /* The "\-c" command\-line option can be used to specify that the
186 while ((opt = getopt(argc, argv, "c")) != \-1) {
197 if (errfnd || optind >= argc) {
198 fprintf(stderr, "Usage: %s [\-c] string...\\n", argv[0]);
202 /* Create first element and place it in the linked list */
204 elem = new_element();
207 elem\->name = argv[optind];
210 elem\->forward = elem;
211 elem\->backward = elem;
217 /* Add remaining command\-line arguments as list elements */
219 while (++optind < argc) {
222 elem = new_element();
223 elem\->name = argv[optind];
227 /* Traverse the list from the start, printing element names */
229 printf("Traversing completed list:\\n");
232 printf(" %s\\n", elem\->name);
233 elem = elem\->forward;
234 } while (elem != NULL && elem != first);
237 printf("That was a circular list\\n");