OSDN Git Service

3b4706603138f2a39937779c561724a869200ddb
[linuxjm/LDP_man-pages.git] / release / man3 / insque.3
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>
4 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
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)
31 .\"
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
35 .\"
36 .\"*******************************************************************
37 .\"
38 .\" This file was generated with po4a. Translate the source file.
39 .\"
40 .\"*******************************************************************
41 .\"
42 .\" Japanese Version Copyright (c) 1998 AKAMATSU Kazuo
43 .\"         all rights reserved.
44 .\" Translated Thu Jan 7 16:00 JST 1999
45 .\"         by AKAMATSU Kazuo
46 .\" Updated Sun Sep 14 JST 2003 by Kentaro Shirakata <argrath@ub32.org>
47 .\" Updated 2012-04-30, Akihiro MOTOKI <amotoki@gmail.com>
48 .\"
49 .TH INSQUE 3 2010\-09\-09 "" "Linux Programmer's Manual"
50 .SH 名前
51 insque, remque \- キューにアイテムを挿入/削除する
52 .SH 書式
53 .nf
54 \fB#include <search.h>\fP
55 .sp
56 \fBvoid insque(void *\fP\fIelem\fP\fB, void *\fP\fIprev\fP\fB);\fP
57
58 \fBvoid remque(void *\fP\fIelem\fP\fB);\fP
59 .fi
60 .sp
61 .in -4n
62 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
63 .in
64 .sp
65 .ad l
66 \fBinsque\fP(), \fBremque\fP():
67 .RS 4
68 _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
69 .RE
70 .ad
71 .SH 説明
72 関数 \fBinsque\fP() と \fBremque\fP() は双方向連結リスト (doubly\-linked list) を
73 操作する。リスト中のそれぞれの要素は、最初の二つの要素がそれぞれ次と前への
74 ポインタであるような構造体である。
75 リンクリストは、線形 (linear) か環状 (circular) のどちらかになる
76 (線形の場合には、リストの末尾では次へのポインタが NULL になり、
77 リストの先頭では前へのポインタが NULL になる)。
78
79 \fBinsque\fP() 関数は \fIelem\fP で示される要素を \fIprev\fP で示される
80 要素の直後に挿入する。
81
82 リストが線形の場合、\fIinsque(elem, NULL)\fP を呼び出すと、
83 リストの最初の要素を挿入することができる。
84 この呼び出しを行うと \fIelem\fP の次へのポインタと前へのポインタに
85 共に NULL が設定される。
86
87 リストが環状の場合、呼び出す側が、最初の要素の次へのポインタと前へのポインタ
88 が自分自身を指し、また  \fBinsque\fP() の呼び出しで \fIprev\fP 引き数が最初の要素
89 を指すように保証しなければならない。
90
91 \fBremque\fP() 関数は \fIelem\fP で示される要素を双方向連結リストから取り除く。
92 .SH 準拠
93 POSIX.1\-2001.
94 .SH 注意
95 伝統的に (SunOS, Linux libc 4,5 では) これらの関数の引き数は \fIstruct qelem
96 *\fP型であり、これは以下のように定義されている。
97
98 .in +4n
99 .nf
100 struct qelem {
101     struct qelem *q_forw;
102     struct qelem *q_back;
103     char          q_data[1];
104 };
105 .fi
106 .in
107
108 この定義は \fI<search.h>\fP をインクルードする前に \fB_GNU_SOURCE\fP を定義することで得られる。
109
110 これらの関数のプロトタイプの置かれる場所は、UNIX の種類により異なる。
111 上記は POSIX 版である。 \fI<string.h>\fP にあるシステムもある。
112 Linux libc4 と libc5 ではプロトタイプは \fI<stdlib.h>\fP に置かれている。
113 .SH バグ
114 glibc 2.4 以前では \fIprev\fP に NULL を指定することができなかった。
115 その結果、線形のリストを作成するためには、
116 呼び出し側は、最初の呼び出しで、リストの最初の 2 つの要素を持ち、
117 各要素の次へのポインタと前へのポインタを適切に初期化したリストを
118 作成しなければならなかった。
119 .SH 例
120 次のプログラムは \fBinsque\fP() の使用法を示したものである。
121 下記はプログラムの実行例である。
122 .in +4n
123 .nf
124
125 $ \fB./a.out \-c a b c\fP
126 Traversing completed list:
127     a
128     b
129     c
130 That was a circular list
131 .fi
132 .in
133 .SS プログラムのソース
134 \&
135 .nf
136 #include <stdio.h>
137 #include <stdlib.h>
138 #include <unistd.h>
139 #include <search.h>
140
141 struct element {
142     struct element *forward;
143     struct element *backward;
144     char *name;
145 };
146
147 static struct element *
148 new_element(void)
149 {
150     struct element *e;
151
152     e = malloc(sizeof(struct element));
153     if (e == NULL) {
154         fprintf(stderr, "malloc() failed\en");
155         exit(EXIT_FAILURE);
156     }
157
158     return e;
159 }
160
161 int
162 main(int argc, char *argv[])
163 {
164     struct element *first, *elem, *prev;
165     int circular, opt, errfnd;
166
167     /* The "\-c" command\-line option can be used to specify that the
168        list is circular */
169
170     errfnd = 0;
171     circular = 0;
172     while ((opt = getopt(argc, argv, "c")) != \-1) {
173         switch (opt) {
174         case 'c':
175             circular = 1;
176             break;
177         default:
178             errfnd = 1;
179             break;
180         }
181     }
182
183     if (errfnd || optind >= argc) {
184         fprintf(stderr,  "Usage: %s [\-c] string...\en", argv[0]);
185         exit(EXIT_FAILURE);
186     }
187
188     /* Create first element and place it in the linked list */
189
190     elem = new_element();
191     first = elem;
192
193     elem\->name = argv[optind];
194
195     if (circular) {
196         elem\->forward = elem;
197         elem\->backward = elem;
198         insque(elem, elem);
199     } else {
200         insque(elem, NULL);
201     }
202
203     /* Add remaining command\-line arguments as list elements */
204
205     while (++optind < argc) {
206         prev = elem;
207
208         elem = new_element();
209         elem\->name = argv[optind];
210         insque(elem, prev);
211     }
212
213     /* Traverse the list from the start, printing element names */
214
215     printf("Traversing completed list:\en");
216     elem = first;
217     do {
218         printf("    %s\en", elem\->name);
219         elem = elem\->forward;
220     } while (elem != NULL && elem != first);
221
222     if (elem == first)
223         printf("That was a circular list\en");
224
225     exit(EXIT_SUCCESS);
226 }
227 .fi
228 .SH この文書について
229 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部
230 である。プロジェクトの説明とバグ報告に関する情報は
231 http://www.kernel.org/doc/man\-pages/ に書かれている。