C 언어의 끝 대기 열 tailq

quue 와 list 의 구조 정의 와 조작 은 모두 'sys / quue. h' 에서 이 루어 졌 고 주로 다음 네 가지 데이터 구 조 를 정의 했다.
  • 단 방향 목록 (single - linked lists)
  • 단 방향 꼬리 대기 열 (single - linked tail queue)
  • 목록 (lists)
  • 꼬리 대기 열 (tail queues)
  • 끝 대기 열 그림
     
    끝 대기 열 상용 매크로
    매크로 이름
    조작 하 다.
    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; }

    좋은 웹페이지 즐겨찾기