C 언어의 끝 대기 열 tailq
2767 단어 프로 그래 밍 기초linux
끝 대기 열 상용 매크로
매크로 이름
조작 하 다.
TAILQ_INIT
대기 열 초기 화
TAILQ_FOREACH
대기 열 을 옮 겨 다 니 기
TAILQ_INSERT_BEFORE
요 소 를 지정 하기 전에 요 소 를 삽입 합 니 다.
TAILQ_INSERT_TAIL
대기 열 끝 에 요 소 를 삽입 합 니 다.
TAILQ_EMPTY
대기 열 이 비어 있 는 지 확인 합 니 다.
TAILQ_REMOVE
대기 열 에서 요 소 를 제거 합 니 다.
사용 예시
#include
#include
#include
/*
,
TAILQ_ENTRY
*/
struct tailq_entry {
int value;
TAILQ_ENTRY(tailq_entry) entries;
};
//
TAILQ_HEAD(, tailq_entry) my_tailq_head;
int main(int argc, char *argv[])
{
//
struct tailq_entry *item;
//
struct tailq_entry *tmp_item;
//
TAILQ_INIT(&my_tailq_head);
int i;
// 10
for(i=0; i<10; i++) {
//
item = malloc(sizeof(*item));
if (item == NULL) {
perror("malloc failed");
exit(-1);
}
//
item->value = i;
/*
1:
2:
3:
*/
TAILQ_INSERT_TAIL(&my_tailq_head, item, entries);
}
//
printf("Forward traversal: ");
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ",item->value);
}
printf("
");
//
printf("Adding new item after 5: ");
TAILQ_FOREACH(item, &my_tailq_head, entries) {
if (item->value == 5) {
struct tailq_entry *new_item = malloc(sizeof(*new_item));
if (new_item == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
new_item->value = 10;
//
TAILQ_INSERT_AFTER(&my_tailq_head, item, new_item, entries);
break;
}
}
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("
");
//
printf("Deleting item with value 3: ");
for(item = TAILQ_FIRST(&my_tailq_head); item != NULL; item = tmp_item) {
if (item->value == 3) {
//
TAILQ_REMOVE(&my_tailq_head, item, entries);
//
free(item);
break;
}
tmp_item = TAILQ_NEXT(item, entries);
}
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("
");
//
while (item = TAILQ_FIRST(&my_tailq_head)) {
TAILQ_REMOVE(&my_tailq_head, item, entries);
free(item);
}
//
if (!TAILQ_EMPTY(&my_tailq_head)) {
printf("tail queue is NOT empty!
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Makinalarda NFS Sunucu Mount Etme(데비안 및 RPM)Debian은 Red Hat 탭과 Linux NFS Sunucu 라이브러리를 모두 지원합니다. NFS 지불 방법은 클라이언트 제작자가 사용하지 않는 기본 설정을 유지하는 것입니다. Örnek verecek olurs...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.