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 .\"*******************************************************************
36 .\" This file was generated with po4a. Translate the source file.
38 .\"*******************************************************************
39 .TH INSQUE 3 2010\-09\-09 "" "Linux Programmer's Manual"
41 insque, remque \- キューにアイテムを挿入/削除する
44 \fB#include <search.h>\fP
46 \fBvoid insque(void *\fP\fIelem\fP\fB, void *\fP\fIprev\fP\fB);\fP
48 \fBvoid remque(void *\fP\fIelem\fP\fB);\fP
52 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
56 \fBinsque\fP(), \fBremque\fP():
58 _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
62 関数 \fBinsque\fP() と \fBremque\fP() は双方向連結リスト (doubly\-linked list) を
63 操作する。リスト中のそれぞれの要素は、最初の二つの要素がそれぞれ次と前への
65 リンクリストは、線形 (linear) か環状 (circular) のどちらかになる
66 (線形の場合には、リストの末尾では次へのポインタが NULL になり、
67 リストの先頭では前へのポインタが NULL になる)。
69 \fBinsque\fP() 関数は \fIelem\fP で示される要素を \fIprev\fP で示される
72 リストが線形の場合、\fIinsque(elem, NULL)\fP を呼び出すと、
74 この呼び出しを行うと \fIelem\fP の次へのポインタと前へのポインタに
77 リストが環状の場合、呼び出す側が、最初の要素の次へのポインタと前へのポインタ
78 が自分自身を指し、また \fBinsque\fP() の呼び出しで \fIprev\fP 引き数が最初の要素
81 \fBremque\fP() 関数は \fIelem\fP で示される要素を双方向連結リストから取り除く。
85 伝統的に (SunOS, Linux libc 4,5 では) これらの関数の引数は \fIstruct qelem
86 *\fP型であり、これは以下のように定義されている。
98 この定義は \fI<search.h>\fP をインクルードする前に \fB_GNU_SOURCE\fP を定義することで得られる。
100 これらの関数のプロトタイプの置かれる場所は、UNIX の種類により異なる。
101 上記は POSIX 版である。 \fI<string.h>\fP にあるシステムもある。
102 Linux libc4 と libc5 ではプロトタイプは \fI<stdlib.h>\fP に置かれている。
104 glibc 2.4 以前では \fIprev\fP に NULL を指定することができなかった。
105 その結果、線形のリストを作成するためには、
106 呼び出し側は、最初の呼び出しで、リストの最初の 2 つの要素を持ち、
107 各要素の次へのポインタと前へのポインタを適切に初期化したリストを
110 次のプログラムは \fBinsque\fP() の使用法を示したものである。
115 $ \fB./a.out \-c a b c\fP
116 Traversing completed list:
120 That was a circular list
132 struct element *forward;
133 struct element *backward;
137 static struct element *
142 e = malloc(sizeof(struct element));
144 fprintf(stderr, "malloc() failed\en");
152 main(int argc, char *argv[])
154 struct element *first, *elem, *prev;
155 int circular, opt, errfnd;
157 /* The "\-c" command\-line option can be used to specify that the
162 while ((opt = getopt(argc, argv, "c")) != \-1) {
173 if (errfnd || optind >= argc) {
174 fprintf(stderr, "Usage: %s [\-c] string...\en", argv[0]);
178 /* Create first element and place it in the linked list */
180 elem = new_element();
183 elem\->name = argv[optind];
186 elem\->forward = elem;
187 elem\->backward = elem;
193 /* Add remaining command\-line arguments as list elements */
195 while (++optind < argc) {
198 elem = new_element();
199 elem\->name = argv[optind];
203 /* Traverse the list from the start, printing element names */
205 printf("Traversing completed list:\en");
208 printf(" %s\en", elem\->name);
209 elem = elem\->forward;
210 } while (elem != NULL && elem != first);
213 printf("That was a circular list\en");