링크 ux 에서 링크 사용
정 리 된 코드 는 다음 과 같 습 니 다.
list.h
#ifndef _LIST_H_
#define _LIST_H_
/**********************************************************
: MEMBER TYPE
**********************************************************/
#define offsetof(TYPE, MEMBER) (unsigned long)(&(((TYPE*)0)->MEMBER))
/**********************************************************
:
:
ptr: type
type:
member:
**********************************************************/
#define container_of(ptr, type, member) (type *)((char*)(ptr) - offsetof(type, member))
#define LIST_HEAD_INIT(name) {&(name), &(name)}
struct list
{
struct list *prev, *next;
};
static inline void list_init(struct list *list)
{
list->next = list;
list->prev = list;
}
static inline int list_empty(struct list *list)
{
return list->next == list;
}
// new_link link
static inline void list_insert(struct list *link, struct list *new_link)
{
new_link->prev = link->prev;
new_link->next = link;
new_link->prev->next = new_link;
new_link->next->prev = new_link;
}
/**********************************************************
: new_link list
**********************************************************/
static inline void list_append(struct list *list, struct list *new_link)
{
list_insert(list, new_link);
}
/**********************************************************
:
**********************************************************/
static inline void list_remove(struct list *link)
{
link->prev->next = link->next;
link->next->prev = link->prev;
}
/**********************************************************
link
link:
type:
member:
**********************************************************/
#define list_entry(link, type, member) container_of(link, type, member)
/**********************************************************
list:
type:
member:
Note:
, ,
**********************************************************/
#define list_head(list, type, member) list_entry((list)->next, type, member)
/**********************************************************
list:
type:
member:
**********************************************************/
#define list_tail(list, type, member) list_entry((list)->prev, type, member)
/**********************************************************
elm:
type:
member: ( )
**********************************************************/
#define list_next(elm,type,member) list_entry((elm)->member.next, type, member)
/**********************************************************
pos :
type :
list :
member : ( )
Note : , ,pos
**********************************************************/
#define list_for_each_entry(pos, type, list, member) \
for (pos = list_head(list, type, member); \
&pos->member != (list); \
pos = list_next(pos, type, member))
/**********************************************************
example function
**********************************************************/
void list_example(void);
#endif
테스트 코드:
list.cpp
#include "stdafx.h"
#include "list.h"
#include <stdlib.h>
struct list g_list = LIST_HEAD_INIT(g_list);
struct test
{
int a;
int b;
char c;
struct list link;
};
void CreateData(int a, int b, char c)
{
struct test *pdata = (struct test *)malloc(sizeof(struct test));
pdata->a = a;
pdata->b = b;
pdata->c = c;
list_append(&g_list, &(pdata->link));
}
void listTest(void)
{
//
CreateData(0,0,0);
CreateData(10,11,12);
CreateData(20,21,22);
CreateData(30,31,32);
CreateData(40,41,42);
//
struct list *templist = &g_list;
struct test *pdata;
pdata = list_entry(templist->next, struct test, link);
templist = templist->next;
pdata = list_entry(templist->next, struct test, link);
// 1
for (templist = g_list.next; templist != &g_list; templist = templist->next)
pdata = list_entry(templist, struct test, link);
// 2
//pdata = list_head(&g_list, struct test, link);
for (pdata = list_head(&g_list, struct test, link); &(pdata->link) != &g_list; pdata = list_next(pdata, struct test, link))
{
pdata->a = 1;
}
// 3
list_for_each_entry(pdata, struct test, &g_list, link)
{
pdata->b = 2;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.