OSDN Git Service

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