insque(3) - wariua/manpages-ko GitHub Wiki
insque, remque - ํ์ ํญ๋ชฉ ์ฝ์ ํ๊ธฐ/์ ๊ฑฐํ๊ธฐ
#include <search.h>
void insque(void *elem, void *prev);
void remque(void *elem);
glibc ๊ธฐ๋ฅ ํ์ธ ๋งคํฌ๋ก ์๊ฑด (feature_test_macros(7) ์ฐธ๊ณ ):
-
insque()
,remque()
: -
_XOPEN_SOURCE >= 500
|| /* Glibc 2.19๋ถํฐ: */ _DEFAULT_SOURCE
|| /* Glibc ๋ฒ์ <= 2.19: */ _SVID_SOURCE
insque()
๋ฐ remque()
ํจ์๋ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์กฐ์ํ๋ค. ๋ฆฌ์คํธ์ ๊ฐ ํญ๋ชฉ์ ์ฒ์ ๋ ์์๊ฐ ์๋ฐฉํฅ ๋ฐ ์ญ๋ฐฉํฅ ํฌ์ธํฐ์ธ ๊ตฌ์กฐ์ฒด์ด๋ค. ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ ์ ํ(์ฆ ๋ฆฌ์คํธ ๋์์ ์๋ฐฉํฅ ํฌ์ธํฐ๊ฐ NULL์ด๊ณ ๋ฆฌ์คํธ ์ฒ์์์ ์ญ๋ฐฉํฅ ํฌ์ธํฐ๊ฐ NULL)์ผ ์๋ ์๊ณ ์ํ์ผ ์๋ ์๋ค.
insque()
ํจ์๋ elem
์ด ๊ฐ๋ฆฌํค๋ ํญ๋ชฉ์ prev
๊ฐ ๊ฐ๋ฆฌํค๋ ํญ๋ชฉ ๋ฐ๋ก ๋ค์์ ์ฝ์
ํ๋ค.
๋ฆฌ์คํธ๊ฐ ์ ํ์ด๋ฉด insque(elem, NULL)
ํธ์ถ์ ์จ์ ๋ฆฌ์คํธ ์ฒซ ํญ๋ชฉ์ ์ฝ์
ํ ์ ์์ผ๋ฉฐ, ๊ทธ ํธ์ถ์์ elem
์ ์ ๋ฐฉํฅ ํฌ์ธํฐ์ ์ญ๋ฐฉํฅ ํฌ์ธํฐ๋ฅผ NULL๋ก ์ค์ ํ๋ค.
๋ฆฌ์คํธ๊ฐ ์ํ์ด๋ฉด ํธ์ถ์๊ฐ ์ฒซ ํญ๋ชฉ์ ์๋ฐฉํฅ ํฌ์ธํฐ์ ์ญ๋ฐฉํฅ ํฌ์ธํฐ๊ฐ ๊ทธ ํญ๋ชฉ์ ๊ฐ๋ฆฌํค๋๋ก ํด์ผ ํ๋ฉฐ, insque()
์ prev
์ธ์๋ ๊ทธ ํญ๋ชฉ์ ๊ฐ๋ฆฌ์ผ์ผ ํ๋ค.
remque()
ํจ์๋ elem
์ด ๊ฐ๋ฆฌํค๋ ํญ๋ชฉ์ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ์์ ์ ๊ฑฐํ๋ค.
์ด ์ ์์ ์ฌ์ฉํ๋ ์ฉ์ด๋ค์ ๋ํ ์ค๋ช ์ attributes(7)๋ฅผ ๋ณด๋ผ.
์ธํฐํ์ด์ค | ์์ฑ | ๊ฐ |
---|---|---|
insque() , remque()
|
์ค๋ ๋ ์์ ์ฑ | MT-Safe |
POSIX.1-2001, POSIX.1-2008.
์์ฃผ ์ค๋๋ ์์คํ
์์๋ ์ด ํจ์๋ค์ ์ธ์๊ฐ ๋ค์์ฒ๋ผ ์ ์๋ struct qelem *
ํ์
์ด์๋ค.
struct qelem {
struct qelem *q_forw;
struct qelem *q_back;
char q_data[1];
};
์์ง๋ <search.h>
ํฌํจ ์ ์ _GNU_SOURCE
๊ฐ ์ ์๋์ด ์์ผ๋ฉด ์ด ํ์
์ ์ป์ ์ ์๋ค.
์ด ํจ์๋ค ์ํ์ ์์น๊ฐ ์ ๋์ค ๋ฒ์ ์ ๋ฐ๋ผ ๋ค๋ฅด๋ค. ์๋ POSIX ๋ฒ์ ์ด๋ค. ์ด๋ค ์์คํ
์์๋ <string.h>
์ ์๋ค.
glibc 2.4 ๋ฐ ์ด์ ์์๋ prev
์ NULL์ ์ง์ ํ๋ ๊ฒ ๋ถ๊ฐ๋ฅํ๋ค. ๊ทธ๋์ ์ ํ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๋ ค๋ฉด ํธ์ถ์๊ฐ ์๋ฐฉํฅ ๋ฐ ์ญ๋ฐฉํฅ ํฌ์ธํฐ๋ฅผ ์ ์ ํ ์ด๊ธฐํ ํ ๋ฆฌ์คํธ์ ์ฒ์ ๋ ํญ๋ชฉ์ผ๋ก ์ต์ด ํธ์ถ์ ํด์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
์๋ ํ๋ก๊ทธ๋จ์ insque()
์ฌ์ฉ ๋ฐฉ์์ ๋ณด์ฌ ์ค๋ค. ๋ค์์ ํ๋ก๊ทธ๋จ ์คํ ์์ด๋ค.
$ ./a.out -c a b c
Traversing completed list:
a
b
c
That was a circular list
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <search.h>
struct element {
struct element *forward;
struct element *backward;
char *name;
};
static struct element *
new_element(void)
{
struct element *e;
e = malloc(sizeof(struct element));
if (e == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
return e;
}
int
main(int argc, char *argv[])
{
struct element *first, *elem, *prev;
int circular, opt, errfnd;
/* "-c" ๋ช
๋ นํ ์ต์
์ ์ฌ์ฉํด ๋ฆฌ์คํธ๋ฅผ ์ํ์ผ๋ก
์ง์ ํ ์ ์๋ค. */
errfnd = 0;
circular = 0;
while ((opt = getopt(argc, argv, "c")) != -1) {
switch (opt) {
case 'c':
circular = 1;
break;
default:
errfnd = 1;
break;
}
}
if (errfnd || optind >= argc) {
fprintf(stderr, "Usage: %s [-c] string...\n", argv[0]);
exit(EXIT_FAILURE);
}
/* ์ฒซ ๋ฒ์งธ ํญ๋ชฉ์ ๋ง๋ค์ด์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ฃ๊ธฐ */
elem = new_element();
first = elem;
elem->name = argv[optind];
if (circular) {
elem->forward = elem;
elem->backward = elem;
insque(elem, elem);
} else {
insque(elem, NULL);
}
/* ๋๋จธ์ง ๋ช
๋ นํ ์ธ์๋ค์ ๋ฆฌ์คํธ ํญ๋ชฉ์ผ๋ก ์ถ๊ฐํ๊ธฐ */
while (++optind < argc) {
prev = elem;
elem = new_element();
elem->name = argv[optind];
insque(elem, prev);
}
/* ๋ฆฌ์คํธ๋ฅผ ์ฒ์๋ถํฐ ์ํํ๋ฉด์ ํญ๋ชฉ ์ด๋ฆ ์ฐ๊ธฐ */
printf("Traversing completed list:\n");
elem = first;
do {
printf(" %s\n", elem->name);
elem = elem->forward;
} while (elem != NULL && elem != first);
if (elem == first)
printf("That was a circular list\n");
exit(EXIT_SUCCESS);
}
2019-03-06