1insque(3)                  Library Functions Manual                  insque(3)
2
3
4

NAME

6       insque, remque - insert/remove an item from a queue
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <search.h>
13
14       void insque(void *elem, void *prev);
15       void remque(void *elem);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       insque(), remque():
20           _XOPEN_SOURCE >= 500
21               || /* glibc >= 2.19: */ _DEFAULT_SOURCE
22               || /* glibc <= 2.19: */ _SVID_SOURCE
23

DESCRIPTION

25       The  insque()  and  remque()  functions manipulate doubly linked lists.
26       Each element in the list is a structure of which the first two elements
27       are  a  forward  and a backward pointer.  The linked list may be linear
28       (i.e., NULL forward pointer at the end of the list  and  NULL  backward
29       pointer at the start of the list) or circular.
30
31       The  insque()  function  inserts the element pointed to by elem immedi‐
32       ately after the element pointed to by prev.
33
34       If the list is linear, then the call insque(elem, NULL) can be used  to
35       insert  the  initial  list  element,  and the call sets the forward and
36       backward pointers of elem to NULL.
37
38       If the list is circular, the caller should ensure that the forward  and
39       backward pointers of the first element are initialized to point to that
40       element, and the prev argument of the insque() call should  also  point
41       to the element.
42
43       The  remque()  function removes the element pointed to by elem from the
44       doubly linked list.
45

ATTRIBUTES

47       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
48       tributes(7).
49
50       ┌────────────────────────────────────────────┬───────────────┬─────────┐
51Interface                                   Attribute     Value   
52       ├────────────────────────────────────────────┼───────────────┼─────────┤
53insque(), remque()                          │ Thread safety │ MT-Safe │
54       └────────────────────────────────────────────┴───────────────┴─────────┘
55

VERSIONS

57       On  ancient  systems,  the  arguments  of  these functions were of type
58       struct qelem *, defined as:
59
60           struct qelem {
61               struct qelem *q_forw;
62               struct qelem *q_back;
63               char          q_data[1];
64           };
65
66       This is still what you will get if _GNU_SOURCE is  defined  before  in‐
67       cluding <search.h>.
68
69       The  location  of the prototypes for these functions differs among sev‐
70       eral versions of UNIX.  The above is the POSIX version.   Some  systems
71       place them in <string.h>.
72

STANDARDS

74       POSIX.1-2008.
75

HISTORY

77       POSIX.1-2001.
78

BUGS

80       In  glibc 2.4 and earlier, it was not possible to specify prev as NULL.
81       Consequently, to build a linear list, the caller had to  build  a  list
82       using  an  initial  call  that  contained the first two elements of the
83       list, with the forward and backward pointers in each  element  suitably
84       initialized.
85

EXAMPLES

87       The program below demonstrates the use of insque().  Here is an example
88       run of the program:
89
90           $ ./a.out -c a b c
91           Traversing completed list:
92               a
93               b
94               c
95           That was a circular list
96
97   Program source
98
99       #include <search.h>
100       #include <stdio.h>
101       #include <stdlib.h>
102       #include <unistd.h>
103
104       struct element {
105           struct element *forward;
106           struct element *backward;
107           char *name;
108       };
109
110       static struct element *
111       new_element(void)
112       {
113           struct element *e;
114
115           e = malloc(sizeof(*e));
116           if (e == NULL) {
117               fprintf(stderr, "malloc() failed\n");
118               exit(EXIT_FAILURE);
119           }
120
121           return e;
122       }
123
124       int
125       main(int argc, char *argv[])
126       {
127           struct element *first, *elem, *prev;
128           int circular, opt, errfnd;
129
130           /* The "-c" command-line option can be used to specify that the
131              list is circular. */
132
133           errfnd = 0;
134           circular = 0;
135           while ((opt = getopt(argc, argv, "c")) != -1) {
136               switch (opt) {
137               case 'c':
138                   circular = 1;
139                   break;
140               default:
141                   errfnd = 1;
142                   break;
143               }
144           }
145
146           if (errfnd || optind >= argc) {
147               fprintf(stderr,  "Usage: %s [-c] string...\n", argv[0]);
148               exit(EXIT_FAILURE);
149           }
150
151           /* Create first element and place it in the linked list. */
152
153           elem = new_element();
154           first = elem;
155
156           elem->name = argv[optind];
157
158           if (circular) {
159               elem->forward = elem;
160               elem->backward = elem;
161               insque(elem, elem);
162           } else {
163               insque(elem, NULL);
164           }
165
166           /* Add remaining command-line arguments as list elements. */
167
168           while (++optind < argc) {
169               prev = elem;
170
171               elem = new_element();
172               elem->name = argv[optind];
173               insque(elem, prev);
174           }
175
176           /* Traverse the list from the start, printing element names. */
177
178           printf("Traversing completed list:\n");
179           elem = first;
180           do {
181               printf("    %s\n", elem->name);
182               elem = elem->forward;
183           } while (elem != NULL && elem != first);
184
185           if (elem == first)
186               printf("That was a circular list\n");
187
188           exit(EXIT_SUCCESS);
189       }
190

SEE ALSO

192       queue(7)
193
194
195
196Linux man-pages 6.04              2023-03-30                         insque(3)
Impressum