链表结构实现
- 数据结构
- 单向链表的实现
- utlist 使用宏实现泛型单向链表
- glist 泛型单向链表
- glist.h
- glist.c
- Linux内核的通用链表
- 『扩展方向』
- 算法
- Leetcode训练指南
- 递归实现链表的基本操作
- 单向链表 + 异或 = 双向链表
- 『扩展方向』
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include
#include
#include
#define SEQLIST_INIT_SIZE 8
#define INC_SIZE 3
typedef int ElemType;
typedef struct SeqList
{
ElemType *base;
int capacity;
int size;
}SeqList;
bool Inc(SeqList *list);
void InitSeqList(SeqList *list);
void push_back(SeqList *list, ElemType x);
void push_front(SeqList *list, ElemType x);
void show_list(SeqList *list);
void pop_back(SeqList *list);
void pop_front(SeqList *list);
void insert_pos(SeqList *list, int pos, ElemType x);
int find(SeqList *list, ElemType key);
int length(SeqList *list);
void delete_pos(SeqList *list, int pos);
void delete_val(SeqList *list, ElemType key);
void sort(SeqList *list);
void resver(SeqList *list);
void clear(SeqList *list);
void destroy(SeqList *list);
void merge(SeqList *lt, SeqList *la, SeqList *lb);
#endif //__SEQLIST_H__
#include"SeqList.h"
bool Inc(SeqList *list)
{
ElemType *newbase = (ElemType*)realloc(list->base,sizeof(ElemType)*(list->capacity+INC_SIZE));
if(newbase == NULL)
{
printf("增配空间失败,内存不足.\n");
return false;
}
list->base = newbase;
list->capacity += INC_SIZE;
return true;
}
void InitSeqList(SeqList *list)
{
list->base = (ElemType *)malloc(sizeof(ElemType) * SEQLIST_INIT_SIZE);
assert(list->base != NULL);
list->capacity = SEQLIST_INIT_SIZE;
list->size = 0;
}
void push_back(SeqList *list, ElemType x)
{
if(list->size >= list->capacity && !Inc(list))
{
printf("顺序表空间已满,%d不能尾部插入数据.\n",x);
return;
}
list->base[list->size] = x;
list->size++;
}
void push_front(SeqList *list, ElemType x)
{
if(list->size >= list->capacity && !Inc(list))
{
printf("顺序表空间已满,%d不能头部插入数据.\n",x);
return;
}
for(int i=list->size; i>0; --i)
{
list->base[i] = list->base[i-1];
}
list->base[0] = x;
list->size++;
}
void show_list(SeqList *list)
{
for(int i=0; i<list->size; ++i)
{
printf("%d ",list->base[i]);
}
printf("\n");
}
void pop_back(SeqList *list)
{
if(list->size == 0)
{
printf("顺序表已空,不能尾部删除数据.\n");
return;
}
list->size--;
}
void pop_front(SeqList *list)
{
if(list->size == 0)
{
printf("顺序表已空,不能尾部删除数据.\n");
return;
}
for(int i=0; i<list->size-1; ++i)
{
list->base[i] = list->base[i+1];
}
list->size--;
}
void insert_pos(SeqList *list, int pos, ElemType x)
{
if(pos<0 || pos>list->size)
{
printf("插入数据的位置非法,不能插入数据.\n");
return;
}
if(list->size >= list->capacity && !Inc(list))
{
printf("顺序表空间已满,%d不能按位置插入数据.\n",x);
return;
}
for(int i=list->size; i>pos; --i)
{
list->base[i] = list->base[i-1];
}
list->base[pos] = x;
list->size++;
}
int find(SeqList *list, ElemType key)
{
for(int i=0; i<list->size; ++i)
{
if(list->base[i] == key)
return i;
}
return -1;
}
int length(SeqList *list)
{
return list->size;
}
void delete_pos(SeqList *list, int pos)
{
if(pos<0 || pos>=list->size)
{
printf("删除数据的位置非法,不能删除数据.\n");
return;
}
for(int i=pos; i<list->size-1; ++i)
{
list->base[i] = list->base[i+1];
}
list->size--;
}
void delete_val(SeqList *list, ElemType key)
{
int pos = find(list,key);
if(pos == -1)
{
printf("要删除的数据不存在.\n");
return;
}
delete_pos(list,pos);
}
void sort(SeqList *list)
{
for(int i=0; i<list->size-1; ++i)
{
for(int j=0; j<list->size-i-1; ++j)
{
if(list->base[j] > list->base[j+1])
{
ElemType tmp = list->base[j];
list->base[j] = list->base[j+1];
list->base[j+1] = tmp;
}
}
}
}
void resver(SeqList *list)
{
if(list->size==0 || list->size==1)
return;
int low = 0;
int high = list->size-1;
ElemType tmp;
while(low < high)
{
tmp = list->base[low];
list->base[low] = list->base[high];
list->base[high] = tmp;
low++;
high--;
}
}
void clear(SeqList *list)
{
list->size = 0;
}
void destroy(SeqList *list)
{
free(list->base);
list->base = NULL;
list->capacity = 0;
list->size = 0;
}
void merge(SeqList *lt, SeqList *la, SeqList *lb)
{
lt->capacity = la->size + lb->size;
lt->base = (ElemType*)malloc(sizeof(ElemType)*lt->capacity);
assert(lt->base != NULL);
int ia = 0;
int ib = 0;
int ic = 0;
while(ia<la->size && ib<lb->size)
{
if(la->base[ia] < lb->base[ib])
lt->base[ic++] = la->base[ia++];
else
lt->base[ic++] = lb->base[ib++];
}
while(ia < la->size)
{
lt->base[ic++] = la->base[ia++];
}
while(ib < lb->size)
{
lt->base[ic++] = lb->base[ib++];
}
lt->size = la->size + lb->size;
}
void insert_pos(SeqList *list, int pos, ElemType x)
{
if(pos<0 || pos>list->size)
{
printf("插入数据的位置非法,不能插入数据.\n");
return;
}
if(pos == 0)
push_front(list,x);
else if(pos == list->size)
push_back(list,x);
else
{
for(int i=list->size; i>pos; --i)
{
list->base[i] = list->base[i-1];
}
list->base[pos] = x;
list->size++;
}
}
#ifndef UTLIST_H
#define UTLIST_H
#define UTLIST_VERSION 2.1.0
#include
/*
* This file contains macros to manipulate singly and doubly-linked lists.
*
* 1. LL_ macros: singly-linked lists.
* 2. DL_ macros: doubly-linked lists.
* 3. CDL_ macros: circular doubly-linked lists.
*
* To use singly-linked lists, your structure must have a "next" pointer.
* To use doubly-linked lists, your structure must "prev" and "next" pointers.
* Either way, the pointer to the head of the list must be initialized to NULL.
*
* ----------------.EXAMPLE -------------------------
* struct item {
* int id;
* struct item *prev, *next;
* }
*
* struct item *list = NULL:
*
* int main() {
* struct item *item;
* ... allocate and populate item ...
* DL_APPEND(list, item);
* }
* --------------------------------------------------
*
* For doubly-linked lists, the append and delete macros are O(1)
* For singly-linked lists, append and delete are O(n) but prepend is O(1)
* The sort macro is O(n log(n)) for all types of single/double/circular lists.
*/
/* These macros use decltype or the earlier __typeof GNU extension.
As decltype is only available in newer compilers (VS2010 or gcc 4.3+
when compiling c++ source) this code uses whatever method is needed
or, for VS2008 where neither is available, uses casting workarounds. */
#if !defined(LDECLTYPE) && !defined(NO_DECLTYPE)
#if defined(_MSC_VER) /* MS compiler */
#if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */
#define LDECLTYPE(x) decltype(x)
#else /* VS2008 or older (or VS2010 in C mode) */
#define NO_DECLTYPE
#endif
#elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__)
#define NO_DECLTYPE
#else /* GNU, Sun and other compilers */
#define LDECLTYPE(x) __typeof(x)
#endif
#endif
/* for VS2008 we use some workarounds to get around the lack of decltype,
* namely, we always reassign our tmp variable to the list head if we need
* to dereference its prev/next pointers, and save/restore the real head.*/
#ifdef NO_DECLTYPE
#define IF_NO_DECLTYPE(x) x
#define LDECLTYPE(x) char*
#define UTLIST_SV(elt,list) _tmp = (char*)(list); {char **_alias = (char**)&(list); *_alias = (elt); }
#define UTLIST_NEXT(elt,list,next) ((char*)((list)->next))
#define UTLIST_NEXTASGN(elt,list,to,next) { char **_alias = (char**)&((list)->next); *_alias=(char*)(to); }
/* #define UTLIST_PREV(elt,list,prev) ((char*)((list)->prev)) */
#define UTLIST_PREVASGN(elt,list,to,prev) { char **_alias = (char**)&((list)->prev); *_alias=(char*)(to); }
#define UTLIST_RS(list) { char **_alias = (char**)&(list); *_alias=_tmp; }
#define UTLIST_CASTASGN(a,b) { char **_alias = (char**)&(a); *_alias=(char*)(b); }
#else
#define IF_NO_DECLTYPE(x)
#define UTLIST_SV(elt,list)
#define UTLIST_NEXT(elt,list,next) ((elt)->next)
#define UTLIST_NEXTASGN(elt,list,to,next) ((elt)->next)=(to)
/* #define UTLIST_PREV(elt,list,prev) ((elt)->prev) */
#define UTLIST_PREVASGN(elt,list,to,prev) ((elt)->prev)=(to)
#define UTLIST_RS(list)
#define UTLIST_CASTASGN(a,b) (a)=(b)
#endif
/******************************************************************************
* The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort *
* Unwieldy variable names used here to avoid shadowing passed-in variables. *
*****************************************************************************/
#define LL_SORT(list, cmp) \
LL_SORT2(list, cmp, next)
#define LL_SORT2(list, cmp, next) \
do { \
LDECLTYPE(list) _ls_p; \
LDECLTYPE(list) _ls_q; \
LDECLTYPE(list) _ls_e; \
LDECLTYPE(list) _ls_tail; \
IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \
int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
if (list) { \
_ls_insize = 1; \
_ls_looping = 1; \
while (_ls_looping) { \
UTLIST_CASTASGN(_ls_p,list); \
(list) = NULL; \
_ls_tail = NULL; \
_ls_nmerges = 0; \
while (_ls_p) { \
_ls_nmerges++; \
_ls_q = _ls_p; \
_ls_psize = 0; \
for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
_ls_psize++; \
UTLIST_SV(_ls_q,list); _ls_q = UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); \
if (!_ls_q) break; \
} \
_ls_qsize = _ls_insize; \
while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
if (_ls_psize == 0) { \
_ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \
UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \
} else if (_ls_qsize == 0 || !_ls_q) { \
_ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \
UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \
} else if (cmp(_ls_p,_ls_q) <= 0) { \
_ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \
UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \
} else { \
_ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \
UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \
} \
if (_ls_tail) { \
UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \
} else { \
UTLIST_CASTASGN(list,_ls_e); \
} \
_ls_tail = _ls_e; \
} \
_ls_p = _ls_q; \
} \
if (_ls_tail) { \
UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,NULL,next); UTLIST_RS(list); \
} \
if (_ls_nmerges <= 1) { \
_ls_looping=0; \
} \
_ls_insize *= 2; \
} \
} \
} while (0)
#define DL_SORT(list, cmp) \
DL_SORT2(list, cmp, prev, next)
#define DL_SORT2(list, cmp, prev, next) \
do { \
LDECLTYPE(list) _ls_p; \
LDECLTYPE(list) _ls_q; \
LDECLTYPE(list) _ls_e; \
LDECLTYPE(list) _ls_tail; \
IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \
int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
if (list) { \
_ls_insize = 1; \
_ls_looping = 1; \
while (_ls_looping) { \
UTLIST_CASTASGN(_ls_p,list); \
(list) = NULL; \
_ls_tail = NULL; \
_ls_nmerges = 0; \
while (_ls_p) { \
_ls_nmerges++; \
_ls_q = _ls_p; \
_ls_psize = 0; \
for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
_ls_psize++; \
UTLIST_SV(_ls_q,list); _ls_q = UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); \
if (!_ls_q) break; \
} \
_ls_qsize = _ls_insize; \
while ((_ls_psize > 0) || ((_ls_qsize > 0) && _ls_q)) { \
if (_ls_psize == 0) { \
_ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \
UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \
} else if ((_ls_qsize == 0) || (!_ls_q)) { \
_ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \
UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \
} else if (cmp(_ls_p,_ls_q) <= 0) { \
_ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \
UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \
} else { \
_ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \
UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \
} \
if (_ls_tail) { \
UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \
} else { \
UTLIST_CASTASGN(list,_ls_e); \
} \
UTLIST_SV(_ls_e,list); UTLIST_PREVASGN(_ls_e,list,_ls_tail,prev); UTLIST_RS(list); \
_ls_tail = _ls_e; \
} \
_ls_p = _ls_q; \
} \
UTLIST_CASTASGN((list)->prev, _ls_tail); \
UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,NULL,next); UTLIST_RS(list); \
if (_ls_nmerges <= 1) { \
_ls_looping=0; \
} \
_ls_insize *= 2; \
} \
} \
} while (0)
#define CDL_SORT(list, cmp) \
CDL_SORT2(list, cmp, prev, next)
#define CDL_SORT2(list, cmp, prev, next) \
do { \
LDECLTYPE(list) _ls_p; \
LDECLTYPE(list) _ls_q; \
LDECLTYPE(list) _ls_e; \
LDECLTYPE(list) _ls_tail; \
LDECLTYPE(list) _ls_oldhead; \
LDECLTYPE(list) _tmp; \
int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \
if (list) { \
_ls_insize = 1; \
_ls_looping = 1; \
while (_ls_looping) { \
UTLIST_CASTASGN(_ls_p,list); \
UTLIST_CASTASGN(_ls_oldhead,list); \
(list) = NULL; \
_ls_tail = NULL; \
_ls_nmerges = 0; \
while (_ls_p) { \
_ls_nmerges++; \
_ls_q = _ls_p; \
_ls_psize = 0; \
for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \
_ls_psize++; \
UTLIST_SV(_ls_q,list); \
if (UTLIST_NEXT(_ls_q,list,next) == _ls_oldhead) { \
_ls_q = NULL; \
} else { \
_ls_q = UTLIST_NEXT(_ls_q,list,next); \
} \
UTLIST_RS(list); \
if (!_ls_q) break; \
} \
_ls_qsize = _ls_insize; \
while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \
if (_ls_psize == 0) { \
_ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \
UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \
if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
} else if (_ls_qsize == 0 || !_ls_q) { \
_ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \
UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \
if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \
} else if (cmp(_ls_p,_ls_q) <= 0) { \
_ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \
UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \
if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \
} else { \
_ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \
UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \
if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \
} \
if (_ls_tail) { \
UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \
} else { \
UTLIST_CASTASGN(list,_ls_e); \
} \
UTLIST_SV(_ls_e,list); UTLIST_PREVASGN(_ls_e,list,_ls_tail,prev); UTLIST_RS(list); \
_ls_tail = _ls_e; \
} \
_ls_p = _ls_q; \
} \
UTLIST_CASTASGN((list)->prev,_ls_tail); \
UTLIST_CASTASGN(_tmp,list); \
UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_tmp,next); UTLIST_RS(list); \
if (_ls_nmerges <= 1) { \
_ls_looping=0; \
} \
_ls_insize *= 2; \
} \
} \
} while (0)
/******************************************************************************
* singly linked list macros (non-circular) *
*****************************************************************************/
#define LL_PREPEND(head,add) \
LL_PREPEND2(head,add,next)
#define LL_PREPEND2(head,add,next) \
do { \
(add)->next = (head); \
(head) = (add); \
} while (0)
#define LL_CONCAT(head1,head2) \
LL_CONCAT2(head1,head2,next)
#define LL_CONCAT2(head1,head2,next) \
do { \
LDECLTYPE(head1) _tmp; \
if (head1) { \
_tmp = (head1); \
while (_tmp->next) { _tmp = _tmp->next; } \
_tmp->next=(head2); \
} else { \
(head1)=(head2); \
} \
} while (0)
#define LL_APPEND(head,add) \
LL_APPEND2(head,add,next)
#define LL_APPEND2(head,add,next) \
do { \
LDECLTYPE(head) _tmp; \
(add)->next=NULL; \
if (head) { \
_tmp = (head); \
while (_tmp->next) { _tmp = _tmp->next; } \
_tmp->next=(add); \
} else { \
(head)=(add); \
} \
} while (0)
#define LL_INSERT_INORDER(head,add,cmp) \
LL_INSERT_INORDER2(head,add,cmp,next)
#define LL_INSERT_INORDER2(head,add,cmp,next) \
do { \
LDECLTYPE(head) _tmp; \
if (head) { \
LL_LOWER_BOUND2(head, _tmp, add, cmp, next); \
LL_APPEND_ELEM2(head, _tmp, add, next); \
} else { \
(head) = (add); \
(head)->next = NULL; \
} \
} while (0)
#define LL_LOWER_BOUND(head,elt,like,cmp) \
LL_LOWER_BOUND2(head,elt,like,cmp,next)
#define LL_LOWER_BOUND2(head,elt,like,cmp,next) \
do { \
if ((head) == NULL || (cmp(head, like)) >= 0) { \
(elt) = NULL; \
} else { \
for ((elt) = (head); (elt)->next != NULL; (elt) = (elt)->next) { \
if (cmp((elt)->next, like) >= 0) { \
break; \
} \
} \
} \
} while (0)
#define LL_DELETE(head,del) \
LL_DELETE2(head,del,next)
#define LL_DELETE2(head,del,next) \
do { \
LDECLTYPE(head) _tmp; \
if ((head) == (del)) { \
(head)=(head)->next; \
} else { \
_tmp = (head); \
while (_tmp->next && (_tmp->next != (del))) { \
_tmp = _tmp->next; \
} \
if (_tmp->next) { \
_tmp->next = (del)->next; \
} \
} \
} while (0)
#define LL_COUNT(head,el,counter) \
LL_COUNT2(head,el,counter,next) \
#define LL_COUNT2(head,el,counter,next) \
do { \
(counter) = 0; \
LL_FOREACH2(head,el,next) { ++(counter); } \
} while (0)
#define LL_FOREACH(head,el) \
LL_FOREACH2(head,el,next)
#define LL_FOREACH2(head,el,next) \
for ((el) = (head); el; (el) = (el)->next)
#define LL_FOREACH_SAFE(head,el,tmp) \
LL_FOREACH_SAFE2(head,el,tmp,next)
#define LL_FOREACH_SAFE2(head,el,tmp,next) \
for ((el) = (head); (el) && ((tmp) = (el)->next, 1); (el) = (tmp))
#define LL_SEARCH_SCALAR(head,out,field,val) \
LL_SEARCH_SCALAR2(head,out,field,val,next)
#define LL_SEARCH_SCALAR2(head,out,field,val,next) \
do { \
LL_FOREACH2(head,out,next) { \
if ((out)->field == (val)) break; \
} \
} while (0)
#define LL_SEARCH(head,out,elt,cmp) \
LL_SEARCH2(head,out,elt,cmp,next)
#define LL_SEARCH2(head,out,elt,cmp,next) \
do { \
LL_FOREACH2(head,out,next) { \
if ((cmp(out,elt))==0) break; \
} \
} while (0)
#define LL_REPLACE_ELEM2(head, el, add, next) \
do { \
LDECLTYPE(head) _tmp; \
assert((head) != NULL); \
assert((el) != NULL); \
assert((add) != NULL); \
(add)->next = (el)->next; \
if ((head) == (el)) { \
(head) = (add); \
} else { \
_tmp = (head); \
while (_tmp->next && (_tmp->next != (el))) { \
_tmp = _tmp->next; \
} \
if (_tmp->next) { \
_tmp->next = (add); \
} \
} \
} while (0)
#define LL_REPLACE_ELEM(head, el, add) \
LL_REPLACE_ELEM2(head, el, add, next)
#define LL_PREPEND_ELEM2(head, el, add, next) \
do { \
if (el) { \
LDECLTYPE(head) _tmp; \
assert((head) != NULL); \
assert((add) != NULL); \
(add)->next = (el); \
if ((head) == (el)) { \
(head) = (add); \
} else { \
_tmp = (head); \
while (_tmp->next && (_tmp->next != (el))) { \
_tmp = _tmp->next; \
} \
if (_tmp->next) { \
_tmp->next = (add); \
} \
} \
} else { \
LL_APPEND2(head, add, next); \
} \
} while (0) \
#define LL_PREPEND_ELEM(head, el, add) \
LL_PREPEND_ELEM2(head, el, add, next)
#define LL_APPEND_ELEM2(head, el, add, next) \
do { \
if (el) { \
assert((head) != NULL); \
assert((add) != NULL); \
(add)->next = (el)->next; \
(el)->next = (add); \
} else { \
LL_PREPEND2(head, add, next); \
} \
} while (0) \
#define LL_APPEND_ELEM(head, el, add) \
LL_APPEND_ELEM2(head, el, add, next)
#ifdef NO_DECLTYPE
/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */
#undef LL_CONCAT2
#define LL_CONCAT2(head1,head2,next) \
do { \
char *_tmp; \
if (head1) { \
_tmp = (char*)(head1); \
while ((head1)->next) { (head1) = (head1)->next; } \
(head1)->next = (head2); \
UTLIST_RS(head1); \
} else { \
(head1)=(head2); \
} \
} while (0)
#undef LL_APPEND2
#define LL_APPEND2(head,add,next) \
do { \
if (head) { \
(add)->next = head; /* use add->next as a temp variable */ \
while ((add)->next->next) { (add)->next = (add)->next->next; } \
(add)->next->next=(add); \
} else { \
(head)=(add); \
} \
(add)->next=NULL; \
} while (0)
#undef LL_INSERT_INORDER2
#define LL_INSERT_INORDER2(head,add,cmp,next) \
do { \
if ((head) == NULL || (cmp(head, add)) >= 0) { \
(add)->next = (head); \
(head) = (add); \
} else { \
char *_tmp = (char*)(head); \
while ((head)->next != NULL && (cmp((head)->next, add)) < 0) { \
(head) = (head)->next; \
} \
(add)->next = (head)->next; \
(head)->next = (add); \
UTLIST_RS(head); \
} \
} while (0)
#undef LL_DELETE2
#define LL_DELETE2(head,del,next) \
do { \
if ((head) == (del)) { \
(head)=(head)->next; \
} else { \
char *_tmp = (char*)(head); \
while ((head)->next && ((head)->next != (del))) { \
(head) = (head)->next; \
} \
if ((head)->next) { \
(head)->next = ((del)->next); \
} \
UTLIST_RS(head); \
} \
} while (0)
#undef LL_REPLACE_ELEM2
#define LL_REPLACE_ELEM2(head, el, add, next) \
do { \
assert((head) != NULL); \
assert((el) != NULL); \
assert((add) != NULL); \
if ((head) == (el)) { \
(head) = (add); \
} else { \
(add)->next = head; \
while ((add)->next->next && ((add)->next->next != (el))) { \
(add)->next = (add)->next->next; \
} \
if ((add)->next->next) { \
(add)->next->next = (add); \
} \
} \
(add)->next = (el)->next; \
} while (0)
#undef LL_PREPEND_ELEM2
#define LL_PREPEND_ELEM2(head, el, add, next) \
do { \
if (el) { \
assert((head) != NULL); \
assert((add) != NULL); \
if ((head) == (el)) { \
(head) = (add); \
} else { \
(add)->next = (head); \
while ((add)->next->next && ((add)->next->next != (el))) { \
(add)->next = (add)->next->next; \
} \
if ((add)->next->next) { \
(add)->next->next = (add); \
} \
} \
(add)->next = (el); \
} else { \
LL_APPEND2(head, add, next); \
} \
} while (0) \
#endif /* NO_DECLTYPE */
/******************************************************************************
* doubly linked list macros (non-circular) *
*****************************************************************************/
#define DL_PREPEND(head,add) \
DL_PREPEND2(head,add,prev,next)
#define DL_PREPEND2(head,add,prev,next) \
do { \
(add)->next = (head); \
if (head) { \
(add)->prev = (head)->prev; \
(head)->prev = (add); \
} else { \
(add)->prev = (add); \
} \
(head) = (add); \
} while (0)
#define DL_APPEND(head,add) \
DL_APPEND2(head,add,prev,next)
#define DL_APPEND2(head,add,prev,next) \
do { \
if (head) { \
(add)->prev = (head)->prev; \
(head)->prev->next = (add); \
(head)->prev = (add); \
(add)->next = NULL; \
} else { \
(head)=(add); \
(head)->prev = (head); \
(head)->next = NULL; \
} \
} while (0)
#define DL_INSERT_INORDER(head,add,cmp) \
DL_INSERT_INORDER2(head,add,cmp,prev,next)
#define DL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
LDECLTYPE(head) _tmp; \
if (head) { \
DL_LOWER_BOUND2(head, _tmp, add, cmp, next); \
DL_APPEND_ELEM2(head, _tmp, add, prev, next); \
} else { \
(head) = (add); \
(head)->prev = (head); \
(head)->next = NULL; \
} \
} while (0)
#define DL_LOWER_BOUND(head,elt,like,cmp) \
DL_LOWER_BOUND2(head,elt,like,cmp,next)
#define DL_LOWER_BOUND2(head,elt,like,cmp,next) \
do { \
if ((head) == NULL || (cmp(head, like)) >= 0) { \
(elt) = NULL; \
} else { \
for ((elt) = (head); (elt)->next != NULL; (elt) = (elt)->next) { \
if ((cmp((elt)->next, like)) >= 0) { \
break; \
} \
} \
} \
} while (0)
#define DL_CONCAT(head1,head2) \
DL_CONCAT2(head1,head2,prev,next)
#define DL_CONCAT2(head1,head2,prev,next) \
do { \
LDECLTYPE(head1) _tmp; \
if (head2) { \
if (head1) { \
UTLIST_CASTASGN(_tmp, (head2)->prev); \
(head2)->prev = (head1)->prev; \
(head1)->prev->next = (head2); \
UTLIST_CASTASGN((head1)->prev, _tmp); \
} else { \
(head1)=(head2); \
} \
} \
} while (0)
#define DL_DELETE(head,del) \
DL_DELETE2(head,del,prev,next)
#define DL_DELETE2(head,del,prev,next) \
do { \
assert((head) != NULL); \
assert((del)->prev != NULL); \
if ((del)->prev == (del)) { \
(head)=NULL; \
} else if ((del)==(head)) { \
(del)->next->prev = (del)->prev; \
(head) = (del)->next; \
} else { \
(del)->prev->next = (del)->next; \
if ((del)->next) { \
(del)->next->prev = (del)->prev; \
} else { \
(head)->prev = (del)->prev; \
} \
} \
} while (0)
#define DL_COUNT(head,el,counter) \
DL_COUNT2(head,el,counter,next) \
#define DL_COUNT2(head,el,counter,next) \
do { \
(counter) = 0; \
DL_FOREACH2(head,el,next) { ++(counter); } \
} while (0)
#define DL_FOREACH(head,el) \
DL_FOREACH2(head,el,next)
#define DL_FOREACH2(head,el,next) \
for ((el) = (head); el; (el) = (el)->next)
/* this version is safe for deleting the elements during iteration */
#define DL_FOREACH_SAFE(head,el,tmp) \
DL_FOREACH_SAFE2(head,el,tmp,next)
#define DL_FOREACH_SAFE2(head,el,tmp,next) \
for ((el) = (head); (el) && ((tmp) = (el)->next, 1); (el) = (tmp))
/* these are identical to their singly-linked list counterparts */
#define DL_SEARCH_SCALAR LL_SEARCH_SCALAR
#define DL_SEARCH LL_SEARCH
#define DL_SEARCH_SCALAR2 LL_SEARCH_SCALAR2
#define DL_SEARCH2 LL_SEARCH2
#define DL_REPLACE_ELEM2(head, el, add, prev, next) \
do { \
assert((head) != NULL); \
assert((el) != NULL); \
assert((add) != NULL); \
if ((head) == (el)) { \
(head) = (add); \
(add)->next = (el)->next; \
if ((el)->next == NULL) { \
(add)->prev = (add); \
} else { \
(add)->prev = (el)->prev; \
(add)->next->prev = (add); \
} \
} else { \
(add)->next = (el)->next; \
(add)->prev = (el)->prev; \
(add)->prev->next = (add); \
if ((el)->next == NULL) { \
(head)->prev = (add); \
} else { \
(add)->next->prev = (add); \
} \
} \
} while (0)
#define DL_REPLACE_ELEM(head, el, add) \
DL_REPLACE_ELEM2(head, el, add, prev, next)
#define DL_PREPEND_ELEM2(head, el, add, prev, next) \
do { \
if (el) { \
assert((head) != NULL); \
assert((add) != NULL); \
(add)->next = (el); \
(add)->prev = (el)->prev; \
(el)->prev = (add); \
if ((head) == (el)) { \
(head) = (add); \
} else { \
(add)->prev->next = (add); \
} \
} else { \
DL_APPEND2(head, add, prev, next); \
} \
} while (0) \
#define DL_PREPEND_ELEM(head, el, add) \
DL_PREPEND_ELEM2(head, el, add, prev, next)
#define DL_APPEND_ELEM2(head, el, add, prev, next) \
do { \
if (el) { \
assert((head) != NULL); \
assert((add) != NULL); \
(add)->next = (el)->next; \
(add)->prev = (el); \
(el)->next = (add); \
if ((add)->next) { \
(add)->next->prev = (add); \
} else { \
(head)->prev = (add); \
} \
} else { \
DL_PREPEND2(head, add, prev, next); \
} \
} while (0) \
#define DL_APPEND_ELEM(head, el, add) \
DL_APPEND_ELEM2(head, el, add, prev, next)
#ifdef NO_DECLTYPE
/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */
#undef DL_INSERT_INORDER2
#define DL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
if ((head) == NULL) { \
(add)->prev = (add); \
(add)->next = NULL; \
(head) = (add); \
} else if ((cmp(head, add)) >= 0) { \
(add)->prev = (head)->prev; \
(add)->next = (head); \
(head)->prev = (add); \
(head) = (add); \
} else { \
char *_tmp = (char*)(head); \
while ((head)->next && (cmp((head)->next, add)) < 0) { \
(head) = (head)->next; \
} \
(add)->prev = (head); \
(add)->next = (head)->next; \
(head)->next = (add); \
UTLIST_RS(head); \
if ((add)->next) { \
(add)->next->prev = (add); \
} else { \
(head)->prev = (add); \
} \
} \
} while (0)
#endif /* NO_DECLTYPE */
/******************************************************************************
* circular doubly linked list macros *
*****************************************************************************/
#define CDL_APPEND(head,add) \
CDL_APPEND2(head,add,prev,next)
#define CDL_APPEND2(head,add,prev,next) \
do { \
if (head) { \
(add)->prev = (head)->prev; \
(add)->next = (head); \
(head)->prev = (add); \
(add)->prev->next = (add); \
} else { \
(add)->prev = (add); \
(add)->next = (add); \
(head) = (add); \
} \
} while (0)
#define CDL_PREPEND(head,add) \
CDL_PREPEND2(head,add,prev,next)
#define CDL_PREPEND2(head,add,prev,next) \
do { \
if (head) { \
(add)->prev = (head)->prev; \
(add)->next = (head); \
(head)->prev = (add); \
(add)->prev->next = (add); \
} else { \
(add)->prev = (add); \
(add)->next = (add); \
} \
(head) = (add); \
} while (0)
#define CDL_INSERT_INORDER(head,add,cmp) \
CDL_INSERT_INORDER2(head,add,cmp,prev,next)
#define CDL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
LDECLTYPE(head) _tmp; \
if (head) { \
CDL_LOWER_BOUND2(head, _tmp, add, cmp, next); \
CDL_APPEND_ELEM2(head, _tmp, add, prev, next); \
} else { \
(head) = (add); \
(head)->next = (head); \
(head)->prev = (head); \
} \
} while (0)
#define CDL_LOWER_BOUND(head,elt,like,cmp) \
CDL_LOWER_BOUND2(head,elt,like,cmp,next)
#define CDL_LOWER_BOUND2(head,elt,like,cmp,next) \
do { \
if ((head) == NULL || (cmp(head, like)) >= 0) { \
(elt) = NULL; \
} else { \
for ((elt) = (head); (elt)->next != (head); (elt) = (elt)->next) { \
if ((cmp((elt)->next, like)) >= 0) { \
break; \
} \
} \
} \
} while (0)
#define CDL_DELETE(head,del) \
CDL_DELETE2(head,del,prev,next)
#define CDL_DELETE2(head,del,prev,next) \
do { \
if (((head)==(del)) && ((head)->next == (head))) { \
(head) = NULL; \
} else { \
(del)->next->prev = (del)->prev; \
(del)->prev->next = (del)->next; \
if ((del) == (head)) (head)=(del)->next; \
} \
} while (0)
#define CDL_COUNT(head,el,counter) \
CDL_COUNT2(head,el,counter,next) \
#define CDL_COUNT2(head, el, counter,next) \
do { \
(counter) = 0; \
CDL_FOREACH2(head,el,next) { ++(counter); } \
} while (0)
#define CDL_FOREACH(head,el) \
CDL_FOREACH2(head,el,next)
#define CDL_FOREACH2(head,el,next) \
for ((el)=(head);el;(el)=(((el)->next==(head)) ? NULL : (el)->next))
#define CDL_FOREACH_SAFE(head,el,tmp1,tmp2) \
CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next)
#define CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next) \
for ((el) = (head), (tmp1) = (head) ? (head)->prev : NULL; \
(el) && ((tmp2) = (el)->next, 1); \
(el) = ((el) == (tmp1) ? NULL : (tmp2)))
#define CDL_SEARCH_SCALAR(head,out,field,val) \
CDL_SEARCH_SCALAR2(head,out,field,val,next)
#define CDL_SEARCH_SCALAR2(head,out,field,val,next) \
do { \
CDL_FOREACH2(head,out,next) { \
if ((out)->field == (val)) break; \
} \
} while (0)
#define CDL_SEARCH(head,out,elt,cmp) \
CDL_SEARCH2(head,out,elt,cmp,next)
#define CDL_SEARCH2(head,out,elt,cmp,next) \
do { \
CDL_FOREACH2(head,out,next) { \
if ((cmp(out,elt))==0) break; \
} \
} while (0)
#define CDL_REPLACE_ELEM2(head, el, add, prev, next) \
do { \
assert((head) != NULL); \
assert((el) != NULL); \
assert((add) != NULL); \
if ((el)->next == (el)) { \
(add)->next = (add); \
(add)->prev = (add); \
(head) = (add); \
} else { \
(add)->next = (el)->next; \
(add)->prev = (el)->prev; \
(add)->next->prev = (add); \
(add)->prev->next = (add); \
if ((head) == (el)) { \
(head) = (add); \
} \
} \
} while (0)
#define CDL_REPLACE_ELEM(head, el, add) \
CDL_REPLACE_ELEM2(head, el, add, prev, next)
#define CDL_PREPEND_ELEM2(head, el, add, prev, next) \
do { \
if (el) { \
assert((head) != NULL); \
assert((add) != NULL); \
(add)->next = (el); \
(add)->prev = (el)->prev; \
(el)->prev = (add); \
(add)->prev->next = (add); \
if ((head) == (el)) { \
(head) = (add); \
} \
} else { \
CDL_APPEND2(head, add, prev, next); \
} \
} while (0)
#define CDL_PREPEND_ELEM(head, el, add) \
CDL_PREPEND_ELEM2(head, el, add, prev, next)
#define CDL_APPEND_ELEM2(head, el, add, prev, next) \
do { \
if (el) { \
assert((head) != NULL); \
assert((add) != NULL); \
(add)->next = (el)->next; \
(add)->prev = (el); \
(el)->next = (add); \
(add)->next->prev = (add); \
} else { \
CDL_PREPEND2(head, add, prev, next); \
} \
} while (0)
#define CDL_APPEND_ELEM(head, el, add) \
CDL_APPEND_ELEM2(head, el, add, prev, next)
#ifdef NO_DECLTYPE
/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */
#undef CDL_INSERT_INORDER2
#define CDL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
if ((head) == NULL) { \
(add)->prev = (add); \
(add)->next = (add); \
(head) = (add); \
} else if ((cmp(head, add)) >= 0) { \
(add)->prev = (head)->prev; \
(add)->next = (head); \
(add)->prev->next = (add); \
(head)->prev = (add); \
(head) = (add); \
} else { \
char *_tmp = (char*)(head); \
while ((char*)(head)->next != _tmp && (cmp((head)->next, add)) < 0) { \
(head) = (head)->next; \
} \
(add)->prev = (head); \
(add)->next = (head)->next; \
(add)->next->prev = (add); \
(head)->next = (add); \
UTLIST_RS(head); \
} \
} while (0)
#endif /* NO_DECLTYPE */
#endif /* UTLIST_H */
#ifndef __G_LIST_H__
#define __G_LIST_H__
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
#error "Only can be included directly."
#endif
#include
#include
G_BEGIN_DECLS
typedef struct _GList GList;
struct _GList
{
gpointer data;
GList *next;
GList *prev;
};
/* Doubly linked lists
*/
GLIB_AVAILABLE_IN_ALL
GList* g_list_alloc (void) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
void g_list_free (GList *list);
GLIB_AVAILABLE_IN_ALL
void g_list_free_1 (GList *list);
#define g_list_free1 g_list_free_1
GLIB_AVAILABLE_IN_ALL
void g_list_free_full (GList *list,
GDestroyNotify free_func);
GLIB_AVAILABLE_IN_ALL
GList* g_list_append (GList *list,
gpointer data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_prepend (GList *list,
gpointer data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_insert (GList *list,
gpointer data,
gint position) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_insert_sorted (GList *list,
gpointer data,
GCompareFunc func) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_insert_sorted_with_data (GList *list,
gpointer data,
GCompareDataFunc func,
gpointer user_data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_insert_before (GList *list,
GList *sibling,
gpointer data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_2_62
GList* g_list_insert_before_link (GList *list,
GList *sibling,
GList *link_) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_concat (GList *list1,
GList *list2) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_remove (GList *list,
gconstpointer data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_remove_all (GList *list,
gconstpointer data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_remove_link (GList *list,
GList *llink) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_delete_link (GList *list,
GList *link_) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_reverse (GList *list) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_copy (GList *list) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_2_34
GList* g_list_copy_deep (GList *list,
GCopyFunc func,
gpointer user_data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_nth (GList *list,
guint n);
GLIB_AVAILABLE_IN_ALL
GList* g_list_nth_prev (GList *list,
guint n);
GLIB_AVAILABLE_IN_ALL
GList* g_list_find (GList *list,
gconstpointer data);
GLIB_AVAILABLE_IN_ALL
GList* g_list_find_custom (GList *list,
gconstpointer data,
GCompareFunc func);
GLIB_AVAILABLE_IN_ALL
gint g_list_position (GList *list,
GList *llink);
GLIB_AVAILABLE_IN_ALL
gint g_list_index (GList *list,
gconstpointer data);
GLIB_AVAILABLE_IN_ALL
GList* g_list_last (GList *list);
GLIB_AVAILABLE_IN_ALL
GList* g_list_first (GList *list);
GLIB_AVAILABLE_IN_ALL
guint g_list_length (GList *list);
GLIB_AVAILABLE_IN_ALL
void g_list_foreach (GList *list,
GFunc func,
gpointer user_data);
GLIB_AVAILABLE_IN_ALL
GList* g_list_sort (GList *list,
GCompareFunc compare_func) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
GList* g_list_sort_with_data (GList *list,
GCompareDataFunc compare_func,
gpointer user_data) G_GNUC_WARN_UNUSED_RESULT;
GLIB_AVAILABLE_IN_ALL
gpointer g_list_nth_data (GList *list,
guint n);
GLIB_AVAILABLE_IN_2_64
void g_clear_list (GList **list_ptr,
GDestroyNotify destroy);
#define g_clear_list(list_ptr, destroy) \
G_STMT_START { \
GList *_list; \
\
_list = *(list_ptr); \
if (_list) \
{ \
*list_ptr = NULL; \
\
if ((destroy) != NULL) \
g_list_free_full (_list, (destroy)); \
else \
g_list_free (_list); \
} \
} G_STMT_END \
GLIB_AVAILABLE_MACRO_IN_2_64
#define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
#define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
G_END_DECLS
#endif /* __G_LIST_H__ */
#include "config.h"
#include "glist.h"
#include "gslice.h"
#include "gmessages.h"
#include "gtestutils.h"
/**
* SECTION:linked_lists_double
* @title: Doubly-Linked Lists
* @short_description: linked lists that can be iterated over in both directions
*
* The #GList structure and its associated functions provide a standard
* doubly-linked list data structure.
*
* Each element in the list contains a piece of data, together with
* pointers which link to the previous and next elements in the list.
* Using these pointers it is possible to move through the list in both
* directions (unlike the singly-linked [GSList][glib-Singly-Linked-Lists],
* which only allows movement through the list in the forward direction).
*
* The double linked list does not keep track of the number of items
* and does not keep track of both the start and end of the list. If
* you want fast access to both the start and the end of the list,
* and/or the number of items in the list, use a
* [GQueue][glib-Double-ended-Queues] instead.
*
* The data contained in each element can be either integer values, by
* using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
* or simply pointers to any type of data.
*
* List elements are allocated from the [slice allocator][glib-Memory-Slices],
* which is more efficient than allocating elements individually.
*
* Note that most of the #GList functions expect to be passed a pointer
* to the first element in the list. The functions which insert
* elements return the new start of the list, which may have changed.
*
* There is no function to create a #GList. %NULL is considered to be
* a valid, empty list so you simply set a #GList* to %NULL to initialize
* it.
*
* To add elements, use g_list_append(), g_list_prepend(),
* g_list_insert() and g_list_insert_sorted().
*
* To visit all elements in the list, use a loop over the list:
* |[
* GList *l;
* for (l = list; l != NULL; l = l->next)
* {
* // do something with l->data
* }
* ]|
*
* To call a function for each element in the list, use g_list_foreach().
*
* To loop over the list and modify it (e.g. remove a certain element)
* a while loop is more appropriate, for example:
* |[
* GList *l = list;
* while (l != NULL)
* {
* GList *next = l->next;
* if (should_be_removed (l))
* {
* // possibly free l->data
* list = g_list_delete_link (list, l);
* }
* l = next;
* }
* ]|
*
* To remove elements, use g_list_remove().
*
* To navigate in a list, use g_list_first(), g_list_last(),
* g_list_next(), g_list_previous().
*
* To find elements in the list use g_list_nth(), g_list_nth_data(),
* g_list_find() and g_list_find_custom().
*
* To find the index of an element use g_list_position() and
* g_list_index().
*
* To free the entire list, use g_list_free() or g_list_free_full().
*/
/**
* GList:
* @data: holds the element's data, which can be a pointer to any kind
* of data, or any integer value using the
* [Type Conversion Macros][glib-Type-Conversion-Macros]
* @next: contains the link to the next element in the list
* @prev: contains the link to the previous element in the list
*
* The #GList struct is used for each element in a doubly-linked list.
**/
/**
* g_list_previous:
* @list: an element in a #GList
*
* A convenience macro to get the previous element in a #GList.
* Note that it is considered perfectly acceptable to access
* @list->prev directly.
*
* Returns: the previous element, or %NULL if there are no previous
* elements
**/
/**
* g_list_next:
* @list: an element in a #GList
*
* A convenience macro to get the next element in a #GList.
* Note that it is considered perfectly acceptable to access
* @list->next directly.
*
* Returns: the next element, or %NULL if there are no more elements
**/
#define _g_list_alloc() g_slice_new (GList)
#define _g_list_alloc0() g_slice_new0 (GList)
#define _g_list_free1(list) g_slice_free (GList, list)
/**
* g_list_alloc:
*
* Allocates space for one #GList element. It is called by
* g_list_append(), g_list_prepend(), g_list_insert() and
* g_list_insert_sorted() and so is rarely used on its own.
*
* Returns: a pointer to the newly-allocated #GList element
**/
GList *
g_list_alloc (void)
{
return _g_list_alloc0 ();
}
/**
* g_list_free:
* @list: a #GList
*
* Frees all of the memory used by a #GList.
* The freed elements are returned to the slice allocator.
*
* If list elements contain dynamically-allocated memory, you should
* either use g_list_free_full() or free them manually first.
*
* It can be combined with g_steal_pointer() to ensure the list head pointer
* is not left dangling:
* |[
* GList *list_of_borrowed_things = …; /* (transfer container) */
* g_list_free (g_steal_pointer (&list_of_borrowed_things));
* ]|
*/
void
g_list_free (GList *list)
{
g_slice_free_chain (GList, list, next);
}
/**
* g_list_free_1:
* @list: a #GList element
*
* Frees one #GList element, but does not update links from the next and
* previous elements in the list, so you should not call this function on an
* element that is currently part of a list.
*
* It is usually used after g_list_remove_link().
*/
/**
* g_list_free1:
*
* Another name for g_list_free_1().
**/
void
g_list_free_1 (GList *list)
{
_g_list_free1 (list);
}
/**
* g_list_free_full:
* @list: a pointer to a #GList
* @free_func: the function to be called to free each element's data
*
* Convenience method, which frees all the memory used by a #GList,
* and calls @free_func on every element's data.
*
* @free_func must not modify the list (eg, by removing the freed
* element from it).
*
* It can be combined with g_steal_pointer() to ensure the list head pointer
* is not left dangling — this also has the nice property that the head pointer
* is cleared before any of the list elements are freed, to prevent double frees
* from @free_func:
* |[
* GList *list_of_owned_things = …; /* (transfer full) (element-type GObject) */
* g_list_free_full (g_steal_pointer (&list_of_owned_things), g_object_unref);
* ]|
*
* Since: 2.28
*/
void
g_list_free_full (GList *list,
GDestroyNotify free_func)
{
g_list_foreach (list, (GFunc) free_func, NULL);
g_list_free (list);
}
/**
* g_list_append:
* @list: a pointer to a #GList
* @data: the data for the new element
*
* Adds a new element on to the end of the list.
*
* Note that the return value is the new start of the list,
* if @list was empty; make sure you store the new value.
*
* g_list_append() has to traverse the entire list to find the end,
* which is inefficient when adding multiple elements. A common idiom
* to avoid the inefficiency is to use g_list_prepend() and reverse
* the list with g_list_reverse() when all elements have been added.
*
* |[
* // Notice that these are initialized to the empty list.
* GList *string_list = NULL, *number_list = NULL;
*
* // This is a list of strings.
* string_list = g_list_append (string_list, "first");
* string_list = g_list_append (string_list, "second");
*
* // This is a list of integers.
* number_list = g_list_append (number_list, GINT_TO_POINTER (27));
* number_list = g_list_append (number_list, GINT_TO_POINTER (14));
* ]|
*
* Returns: either @list or the new start of the #GList if @list was %NULL
*/
GList *
g_list_append (GList *list,
gpointer data)
{
GList *new_list;
GList *last;
new_list = _g_list_alloc ();
new_list->data = data;
new_list->next = NULL;
if (list)
{
last = g_list_last (list);
/* g_assert (last != NULL); */
last->next = new_list;
new_list->prev = last;
return list;
}
else
{
new_list->prev = NULL;
return new_list;
}
}
/**
* g_list_prepend:
* @list: a pointer to a #GList, this must point to the top of the list
* @data: the data for the new element
*
* Prepends a new element on to the start of the list.
*
* Note that the return value is the new start of the list,
* which will have changed, so make sure you store the new value.
*
* |[
* // Notice that it is initialized to the empty list.
* GList *list = NULL;
*
* list = g_list_prepend (list, "last");
* list = g_list_prepend (list, "first");
* ]|
*
* Do not use this function to prepend a new element to a different
* element than the start of the list. Use g_list_insert_before() instead.
*
* Returns: a pointer to the newly prepended element, which is the new
* start of the #GList
*/
GList *
g_list_prepend (GList *list,
gpointer data)
{
GList *new_list;
new_list = _g_list_alloc ();
new_list->data = data;
new_list->next = list;
if (list)
{
new_list->prev = list->prev;
if (list->prev)
list->prev->next = new_list;
list->prev = new_list;
}
else
new_list->prev = NULL;
return new_list;
}
/**
* g_list_insert:
* @list: a pointer to a #GList, this must point to the top of the list
* @data: the data for the new element
* @position: the position to insert the element. If this is
* negative, or is larger than the number of elements in the
* list, the new element is added on to the end of the list.
*
* Inserts a new element into the list at the given position.
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_insert (GList *list,
gpointer data,
gint position)
{
GList *new_list;
GList *tmp_list;
if (position < 0)
return g_list_append (list, data);
else if (position == 0)
return g_list_prepend (list, data);
tmp_list = g_list_nth (list, position);
if (!tmp_list)
return g_list_append (list, data);
new_list = _g_list_alloc ();
new_list->data = data;
new_list->prev = tmp_list->prev;
tmp_list->prev->next = new_list;
new_list->next = tmp_list;
tmp_list->prev = new_list;
return list;
}
/**
* g_list_insert_before_link:
* @list: a pointer to a #GList, this must point to the top of the list
* @sibling: (nullable): the list element before which the new element
* is inserted or %NULL to insert at the end of the list
* @link_: the list element to be added, which must not be part of
* any other list
*
* Inserts @link_ into the list before the given position.
*
* Returns: the (possibly changed) start of the #GList
*
* Since: 2.62
*/
GList *
g_list_insert_before_link (GList *list,
GList *sibling,
GList *link_)
{
g_return_val_if_fail (link_ != NULL, list);
g_return_val_if_fail (link_->prev == NULL, list);
g_return_val_if_fail (link_->next == NULL, list);
if (list == NULL)
{
g_return_val_if_fail (sibling == NULL, list);
return link_;
}
else if (sibling != NULL)
{
link_->prev = sibling->prev;
link_->next = sibling;
sibling->prev = link_;
if (link_->prev != NULL)
{
link_->prev->next = link_;
return list;
}
else
{
g_return_val_if_fail (sibling == list, link_);
return link_;
}
}
else
{
GList *last;
for (last = list; last->next != NULL; last = last->next) {}
last->next = link_;
last->next->prev = last;
last->next->next = NULL;
return list;
}
}
/**
* g_list_insert_before:
* @list: a pointer to a #GList, this must point to the top of the list
* @sibling: the list element before which the new element
* is inserted or %NULL to insert at the end of the list
* @data: the data for the new element
*
* Inserts a new element into the list before the given position.
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_insert_before (GList *list,
GList *sibling,
gpointer data)
{
if (list == NULL)
{
list = g_list_alloc ();
list->data = data;
g_return_val_if_fail (sibling == NULL, list);
return list;
}
else if (sibling != NULL)
{
GList *node;
node = _g_list_alloc ();
node->data = data;
node->prev = sibling->prev;
node->next = sibling;
sibling->prev = node;
if (node->prev != NULL)
{
node->prev->next = node;
return list;
}
else
{
g_return_val_if_fail (sibling == list, node);
return node;
}
}
else
{
GList *last;
for (last = list; last->next != NULL; last = last->next) {}
last->next = _g_list_alloc ();
last->next->data = data;
last->next->prev = last;
last->next->next = NULL;
return list;
}
}
/**
* g_list_concat:
* @list1: a #GList, this must point to the top of the list
* @list2: the #GList to add to the end of the first #GList,
* this must point to the top of the list
*
* Adds the second #GList onto the end of the first #GList.
* Note that the elements of the second #GList are not copied.
* They are used directly.
*
* This function is for example used to move an element in the list.
* The following example moves an element to the top of the list:
* |[
* list = g_list_remove_link (list, llink);
* list = g_list_concat (llink, list);
* ]|
*
* Returns: the start of the new #GList, which equals @list1 if not %NULL
*/
GList *
g_list_concat (GList *list1,
GList *list2)
{
GList *tmp_list;
if (list2)
{
tmp_list = g_list_last (list1);
if (tmp_list)
tmp_list->next = list2;
else
list1 = list2;
list2->prev = tmp_list;
}
return list1;
}
static inline GList *
_g_list_remove_link (GList *list,
GList *link)
{
if (link == NULL)
return list;
if (link->prev)
{
if (link->prev->next == link)
link->prev->next = link->next;
else
g_warning ("corrupted double-linked list detected");
}
if (link->next)
{
if (link->next->prev == link)
link->next->prev = link->prev;
else
g_warning ("corrupted double-linked list detected");
}
if (link == list)
list = list->next;
link->next = NULL;
link->prev = NULL;
return list;
}
/**
* g_list_remove:
* @list: a #GList, this must point to the top of the list
* @data: the data of the element to remove
*
* Removes an element from a #GList.
* If two elements contain the same data, only the first is removed.
* If none of the elements contain the data, the #GList is unchanged.
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_remove (GList *list,
gconstpointer data)
{
GList *tmp;
tmp = list;
while (tmp)
{
if (tmp->data != data)
tmp = tmp->next;
else
{
list = _g_list_remove_link (list, tmp);
_g_list_free1 (tmp);
break;
}
}
return list;
}
/**
* g_list_remove_all:
* @list: a #GList, this must point to the top of the list
* @data: data to remove
*
* Removes all list nodes with data equal to @data.
* Returns the new head of the list. Contrast with
* g_list_remove() which removes only the first node
* matching the given data.
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_remove_all (GList *list,
gconstpointer data)
{
GList *tmp = list;
while (tmp)
{
if (tmp->data != data)
tmp = tmp->next;
else
{
GList *next = tmp->next;
if (tmp->prev)
tmp->prev->next = next;
else
list = next;
if (next)
next->prev = tmp->prev;
_g_list_free1 (tmp);
tmp = next;
}
}
return list;
}
/**
* g_list_remove_link:
* @list: a #GList, this must point to the top of the list
* @llink: an element in the #GList
*
* Removes an element from a #GList, without freeing the element.
* The removed element's prev and next links are set to %NULL, so
* that it becomes a self-contained list with one element.
*
* This function is for example used to move an element in the list
* (see the example for g_list_concat()) or to remove an element in
* the list before freeing its data:
* |[
* list = g_list_remove_link (list, llink);
* free_some_data_that_may_access_the_list_again (llink->data);
* g_list_free (llink);
* ]|
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_remove_link (GList *list,
GList *llink)
{
return _g_list_remove_link (list, llink);
}
/**
* g_list_delete_link:
* @list: a #GList, this must point to the top of the list
* @link_: node to delete from @list
*
* Removes the node link_ from the list and frees it.
* Compare this to g_list_remove_link() which removes the node
* without freeing it.
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_delete_link (GList *list,
GList *link_)
{
list = _g_list_remove_link (list, link_);
_g_list_free1 (link_);
return list;
}
/**
* g_list_copy:
* @list: a #GList, this must point to the top of the list
*
* Copies a #GList.
*
* Note that this is a "shallow" copy. If the list elements
* consist of pointers to data, the pointers are copied but
* the actual data is not. See g_list_copy_deep() if you need
* to copy the data as well.
*
* Returns: the start of the new list that holds the same data as @list
*/
GList *
g_list_copy (GList *list)
{
return g_list_copy_deep (list, NULL, NULL);
}
/**
* g_list_copy_deep:
* @list: a #GList, this must point to the top of the list
* @func: a copy function used to copy every element in the list
* @user_data: user data passed to the copy function @func, or %NULL
*
* Makes a full (deep) copy of a #GList.
*
* In contrast with g_list_copy(), this function uses @func to make
* a copy of each list element, in addition to copying the list
* container itself.
*
* @func, as a #GCopyFunc, takes two arguments, the data to be copied
* and a @user_data pointer. On common processor architectures, it's safe to
* pass %NULL as @user_data if the copy function takes only one argument. You
* may get compiler warnings from this though if compiling with GCC’s
* `-Wcast-function-type` warning.
*
* For instance, if @list holds a list of GObjects, you can do:
* |[
* another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
* ]|
*
* And, to entirely free the new list, you could do:
* |[
* g_list_free_full (another_list, g_object_unref);
* ]|
*
* Returns: the start of the new list that holds a full copy of @list,
* use g_list_free_full() to free it
*
* Since: 2.34
*/
GList *
g_list_copy_deep (GList *list,
GCopyFunc func,
gpointer user_data)
{
GList *new_list = NULL;
if (list)
{
GList *last;
new_list = _g_list_alloc ();
if (func)
new_list->data = func (list->data, user_data);
else
new_list->data = list->data;
new_list->prev = NULL;
last = new_list;
list = list->next;
while (list)
{
last->next = _g_list_alloc ();
last->next->prev = last;
last = last->next;
if (func)
last->data = func (list->data, user_data);
else
last->data = list->data;
list = list->next;
}
last->next = NULL;
}
return new_list;
}
/**
* g_list_reverse:
* @list: a #GList, this must point to the top of the list
*
* Reverses a #GList.
* It simply switches the next and prev pointers of each element.
*
* Returns: the start of the reversed #GList
*/
GList *
g_list_reverse (GList *list)
{
GList *last;
last = NULL;
while (list)
{
last = list;
list = last->next;
last->next = last->prev;
last->prev = list;
}
return last;
}
/**
* g_list_nth:
* @list: a #GList, this must point to the top of the list
* @n: the position of the element, counting from 0
*
* Gets the element at the given position in a #GList.
*
* This iterates over the list until it reaches the @n-th position. If you
* intend to iterate over every element, it is better to use a for-loop as
* described in the #GList introduction.
*
* Returns: the element, or %NULL if the position is off
* the end of the #GList
*/
GList *
g_list_nth (GList *list,
guint n)
{
while ((n-- > 0) && list)
list = list->next;
return list;
}
/**
* g_list_nth_prev:
* @list: a #GList
* @n: the position of the element, counting from 0
*
* Gets the element @n places before @list.
*
* Returns: the element, or %NULL if the position is
* off the end of the #GList
*/
GList *
g_list_nth_prev (GList *list,
guint n)
{
while ((n-- > 0) && list)
list = list->prev;
return list;
}
/**
* g_list_nth_data:
* @list: a #GList, this must point to the top of the list
* @n: the position of the element
*
* Gets the data of the element at the given position.
*
* This iterates over the list until it reaches the @n-th position. If you
* intend to iterate over every element, it is better to use a for-loop as
* described in the #GList introduction.
*
* Returns: the element's data, or %NULL if the position
* is off the end of the #GList
*/
gpointer
g_list_nth_data (GList *list,
guint n)
{
while ((n-- > 0) && list)
list = list->next;
return list ? list->data : NULL;
}
/**
* g_list_find:
* @list: a #GList, this must point to the top of the list
* @data: the element data to find
*
* Finds the element in a #GList which contains the given data.
*
* Returns: the found #GList element, or %NULL if it is not found
*/
GList *
g_list_find (GList *list,
gconstpointer data)
{
while (list)
{
if (list->data == data)
break;
list = list->next;
}
return list;
}
/**
* g_list_find_custom:
* @list: a #GList, this must point to the top of the list
* @data: user data passed to the function
* @func: the function to call for each element.
* It should return 0 when the desired element is found
*
* Finds an element in a #GList, using a supplied function to
* find the desired element. It iterates over the list, calling
* the given function which should return 0 when the desired
* element is found. The function takes two #gconstpointer arguments,
* the #GList element's data as the first argument and the
* given user data.
*
* Returns: the found #GList element, or %NULL if it is not found
*/
GList *
g_list_find_custom (GList *list,
gconstpointer data,
GCompareFunc func)
{
g_return_val_if_fail (func != NULL, list);
while (list)
{
if (! func (list->data, data))
return list;
list = list->next;
}
return NULL;
}
/**
* g_list_position:
* @list: a #GList, this must point to the top of the list
* @llink: an element in the #GList
*
* Gets the position of the given element
* in the #GList (starting from 0).
*
* Returns: the position of the element in the #GList,
* or -1 if the element is not found
*/
gint
g_list_position (GList *list,
GList *llink)
{
gint i;
i = 0;
while (list)
{
if (list == llink)
return i;
i++;
list = list->next;
}
return -1;
}
/**
* g_list_index:
* @list: a #GList, this must point to the top of the list
* @data: the data to find
*
* Gets the position of the element containing
* the given data (starting from 0).
*
* Returns: the index of the element containing the data,
* or -1 if the data is not found
*/
gint
g_list_index (GList *list,
gconstpointer data)
{
gint i;
i = 0;
while (list)
{
if (list->data == data)
return i;
i++;
list = list->next;
}
return -1;
}
/**
* g_list_last:
* @list: any #GList element
*
* Gets the last element in a #GList.
*
* Returns: the last element in the #GList,
* or %NULL if the #GList has no elements
*/
GList *
g_list_last (GList *list)
{
if (list)
{
while (list->next)
list = list->next;
}
return list;
}
/**
* g_list_first:
* @list: any #GList element
*
* Gets the first element in a #GList.
*
* Returns: the first element in the #GList,
* or %NULL if the #GList has no elements
*/
GList *
g_list_first (GList *list)
{
if (list)
{
while (list->prev)
list = list->prev;
}
return list;
}
/**
* g_list_length:
* @list: a #GList, this must point to the top of the list
*
* Gets the number of elements in a #GList.
*
* This function iterates over the whole list to count its elements.
* Use a #GQueue instead of a GList if you regularly need the number
* of items. To check whether the list is non-empty, it is faster to check
* @list against %NULL.
*
* Returns: the number of elements in the #GList
*/
guint
g_list_length (GList *list)
{
guint length;
length = 0;
while (list)
{
length++;
list = list->next;
}
return length;
}
/**
* g_list_foreach:
* @list: a #GList, this must point to the top of the list
* @func: the function to call with each element's data
* @user_data: user data to pass to the function
*
* Calls a function for each element of a #GList.
*
* It is safe for @func to remove the element from @list, but it must
* not modify any part of the list after that element.
*/
/**
* GFunc:
* @data: the element's data
* @user_data: user data passed to g_list_foreach() or g_slist_foreach()
*
* Specifies the type of functions passed to g_list_foreach() and
* g_slist_foreach().
*/
void
g_list_foreach (GList *list,
GFunc func,
gpointer user_data)
{
while (list)
{
GList *next = list->next;
(*func) (list->data, user_data);
list = next;
}
}
static GList*
g_list_insert_sorted_real (GList *list,
gpointer data,
GFunc func,
gpointer user_data)
{
GList *tmp_list = list;
GList *new_list;
gint cmp;
g_return_val_if_fail (func != NULL, list);
if (!list)
{
new_list = _g_list_alloc0 ();
new_list->data = data;
return new_list;
}
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
while ((tmp_list->next) && (cmp > 0))
{
tmp_list = tmp_list->next;
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
}
new_list = _g_list_alloc0 ();
new_list->data = data;
if ((!tmp_list->next) && (cmp > 0))
{
tmp_list->next = new_list;
new_list->prev = tmp_list;
return list;
}
if (tmp_list->prev)
{
tmp_list->prev->next = new_list;
new_list->prev = tmp_list->prev;
}
new_list->next = tmp_list;
tmp_list->prev = new_list;
if (tmp_list == list)
return new_list;
else
return list;
}
/**
* g_list_insert_sorted:
* @list: a pointer to a #GList, this must point to the top of the
* already sorted list
* @data: the data for the new element
* @func: the function to compare elements in the list. It should
* return a number > 0 if the first parameter comes after the
* second parameter in the sort order.
*
* Inserts a new element into the list, using the given comparison
* function to determine its position.
*
* If you are adding many new elements to a list, and the number of
* new elements is much larger than the length of the list, use
* g_list_prepend() to add the new items and sort the list afterwards
* with g_list_sort().
*
* Returns: the (possibly changed) start of the #GList
*/
GList *
g_list_insert_sorted (GList *list,
gpointer data,
GCompareFunc func)
{
return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
}
/**
* g_list_insert_sorted_with_data:
* @list: a pointer to a #GList, this must point to the top of the
* already sorted list
* @data: the data for the new element
* @func: the function to compare elements in the list. It should
* return a number > 0 if the first parameter comes after the
* second parameter in the sort order.
* @user_data: user data to pass to comparison function
*
* Inserts a new element into the list, using the given comparison
* function to determine its position.
*
* If you are adding many new elements to a list, and the number of
* new elements is much larger than the length of the list, use
* g_list_prepend() to add the new items and sort the list afterwards
* with g_list_sort().
*
* Returns: the (possibly changed) start of the #GList
*
* Since: 2.10
*/
GList *
g_list_insert_sorted_with_data (GList *list,
gpointer data,
GCompareDataFunc func,
gpointer user_data)
{
return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
}
static GList *
g_list_sort_merge (GList *l1,
GList *l2,
GFunc compare_func,
gpointer user_data)
{
GList list, *l, *lprev;
gint cmp;
l = &list;
lprev = NULL;
while (l1 && l2)
{
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
if (cmp <= 0)
{
l->next = l1;
l1 = l1->next;
}
else
{
l->next = l2;
l2 = l2->next;
}
l = l->next;
l->prev = lprev;
lprev = l;
}
l->next = l1 ? l1 : l2;
l->next->prev = l;
return list.next;
}
static GList *
g_list_sort_real (GList *list,
GFunc compare_func,
gpointer user_data)
{
GList *l1, *l2;
if (!list)
return NULL;
if (!list->next)
return list;
l1 = list;
l2 = list->next;
while ((l2 = l2->next) != NULL)
{
if ((l2 = l2->next) == NULL)
break;
l1 = l1->next;
}
l2 = l1->next;
l1->next = NULL;
return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
g_list_sort_real (l2, compare_func, user_data),
compare_func,
user_data);
}
/**
* g_list_sort:
* @list: a #GList, this must point to the top of the list
* @compare_func: the comparison function used to sort the #GList.
* This function is passed the data from 2 elements of the #GList
* and should return 0 if they are equal, a negative value if the
* first element comes before the second, or a positive value if
* the first element comes after the second.
*
* Sorts a #GList using the given comparison function. The algorithm
* used is a stable sort.
*
* Returns: the (possibly changed) start of the #GList
*/
/**
* GCompareFunc:
* @a: a value
* @b: a value to compare with
*
* Specifies the type of a comparison function used to compare two
* values. The function should return a negative integer if the first
* value comes before the second, 0 if they are equal, or a positive
* integer if the first value comes after the second.
*
* Returns: negative value if @a < @b; zero if @a = @b; positive
* value if @a > @b
*/
GList *
g_list_sort (GList *list,
GCompareFunc compare_func)
{
return g_list_sort_real (list, (GFunc) compare_func, NULL);
}
/**
* g_list_sort_with_data:
* @list: a #GList, this must point to the top of the list
* @compare_func: comparison function
* @user_data: user data to pass to comparison function
*
* Like g_list_sort(), but the comparison function accepts
* a user data argument.
*
* Returns: the (possibly changed) start of the #GList
*/
/**
* GCompareDataFunc:
* @a: a value
* @b: a value to compare with
* @user_data: user data
*
* Specifies the type of a comparison function used to compare two
* values. The function should return a negative integer if the first
* value comes before the second, 0 if they are equal, or a positive
* integer if the first value comes after the second.
*
* Returns: negative value if @a < @b; zero if @a = @b; positive
* value if @a > @b
*/
GList *
g_list_sort_with_data (GList *list,
GCompareDataFunc compare_func,
gpointer user_data)
{
return g_list_sort_real (list, (GFunc) compare_func, user_data);
}
/**
* g_clear_list: (skip)
* @list_ptr: (not nullable): a #GList return location
* @destroy: (nullable): the function to pass to g_list_free_full() or %NULL to not free elements
*
* Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
*
* @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
*
* Since: 2.64
*/
void
(g_clear_list) (GList **list_ptr,
GDestroyNotify destroy)
{
GList *list;
list = *list_ptr;
if (list)
{
*list_ptr = NULL;
if (destroy)
g_list_free_full (list, destroy);
else
g_list_free (list);
}
}
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
struct list_head {
struct list_head *next, *prev;
};
struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
// WRITE_ONCE(prev->next, next);
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
#else
extern void __list_del_entry(struct list_head *entry);
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del_entry(list);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del_entry(list);
list_add_tail(list, head);
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
/**
* list_rotate_left - rotate the list to the left
* @head: the head of the list
*/
static inline void list_rotate_left(struct list_head *head)
{
struct list_head *first;
if (!list_empty(head)) {
first = head->next;
list_move_tail(first, head);
}
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
if (list_empty(head))
return;
if (list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if (entry == head)
INIT_LIST_HEAD(list);
else
__list_cut_position(list, head, entry);
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_head within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member), \
n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_head within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
* too wasteful.
* You lose the ability to access the tail in O(1).
*/
#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
h->next = NULL;
h->pprev = NULL;
}
static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
static inline int hlist_empty(const struct hlist_head *h)
{
return !h->first;
}
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
// WRITE_ONCE(*pprev, next);
if (next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
static inline void hlist_del_init(struct hlist_node *n)
{
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
struct hlist_node *next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
static inline void hlist_add_behind(struct hlist_node *n,
struct hlist_node *prev)
{
n->next = prev->next;
prev->next = n;
n->pprev = &prev->next;
if (n->next)
n->next->pprev = &n->next;
}
/* after that we'll appear to be on some hlist and hlist_del will work */
static inline void hlist_add_fake(struct hlist_node *n)
{
n->pprev = &n->next;
}
//static inline bool hlist_fake(struct hlist_node *h)
//{
// return h->pprev == &h->next;
//}
/*
* Move a list from one list head to another. Fixup the pprev
* reference of the first entry if it exists.
*/
static inline void hlist_move_list(struct hlist_head *old,
struct hlist_head *new)
{
new->first = old->first;
if (new->first)
new->first->pprev = &new->first;
old->first = NULL;
}
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos ; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
#define hlist_entry_safe(ptr, type, member) \
({ typeof(ptr) ____ptr = (ptr); \
____ptr ? hlist_entry(____ptr, type, member) : NULL; \
})
/**
* hlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(pos, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(pos, member) \
for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_from(pos, member) \
for (; pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(pos, n, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
pos && ({ n = pos->member.next; 1; }); \
pos = hlist_entry_safe(n, typeof(*pos), member))
#endif
《双链表》
LC: 21、24、25、61、82、92、143、147、148、203、206、234
typedef struct listnode
{
int val;
struct listnode *next;
}List;
// 结点个数
int count_listnode(List *head)
{
static int count = 0;
return head ? count_listnode( head->next ), ++count: 0;
}
// 反向打印
void bkprint_listnode(List *head)
{
if(head != NULL)
bkprint_listnode(head->next),
printf("%2d ",head->val);
}
// 查找 val 位置
int find( List * head, int val )
{
static int cnt = 0;
if( head == NULL )
return -1;
else if( head->val == val )
return cnt;
else
cnt++,
find( head->next, val );
}
// 删除所有val = d的节点
List* delete_allnode(List *head, int d)
{
if( head == NULL )
return NULL;
head->next = delete_allnode( head->next, d );
return head->val == d ? head->next : head;
}
记录在《双链表》的单链表代双链表,不重述了。
《双链表》