/*!\fn int list_push( LIST *list , void *ptr , void (*destructor)( void *ptr ) ) *\brief Add a pointer to the end of the list. *\param list An initilized list container. A null value will cause an error and a _log message. *\param ptr The pointer you wan to put in the list. A null value will cause an error and a _log message. *\param destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't. *\return TRUE or FALSE. Check error messages in DEBUG mode. */ int list_push( LIST *list , void *ptr , void (*destructor)( void *ptr ) ) { LIST_NODE *node = NULL ; n_assert( list , n_log( LOG_ERR , "invalid list: NULL" ); return FALSE ); n_assert( ptr , n_log( LOG_ERR , "invalid ptr: NULL" ); return FALSE ); if( list -> nb_max_items > 0 && ( list -> nb_items >= list -> nb_max_items ) ) { n_log( LOG_ERR , "list is full" ); return FALSE ; } node = new_list_node( ptr , destructor ); n_assert( node , n_log( LOG_ERR , "Couldn't allocate new node" ); return FALSE ); list -> nb_items ++ ; if( list -> end ) { list -> end -> next = node ; node -> prev = list -> end ; list -> end = node ; } else { list -> start = list -> end = node ; } return TRUE ; } /* list_push( ... ) */
int ylist_insert(ylist_t *l, uint64_t index, void *e) { if (!l->head && !l->tail) { return ylist_add(l, e); } ylist_node *node = find_node_index(l, index); if (!node) { return 1; } ylist_node *new_node = new_list_node(e); if (!node->pre) { new_node->next = node; node->pre = new_node; l->head = new_node; l->cur = l->head; } else { node->pre->next = new_node; new_node->next = node; new_node->pre = node->pre; node->pre = new_node; } l->count++; return 0; }
auto parse(State&& state) const { if (state.cursor) { char32_t value = *state.cursor; if (first <= value && value <= last) { return make_state(new_list_node(value, std::move(state.values)), std::move(state.cursor).next()); } } throw failure{}; }
/*在单链表尾部添加元素*/ void slist_push_back(slist_t *l,void *item) { /*构造新结点*/ list_node_t *node=new_list_node(item); if(l->head){ list_node_t *cur=l->head; while(cur->next){ cur=cur->next; } cur->next=node; } else { l->head=node; } l->n++; }
int ylist_add(ylist_t *list, void *e) { if (!e) { return 1; } ylist_node *node = new_list_node(e); if (!list->head && !list->tail) { list->cur = list->head = list->tail = node; } else { list->tail->next = node; node->pre = list->tail; list->tail = node; } list->count++; return 0; }
/*!\fn int list_push_sorted( LIST *list , void *ptr , int (*comparator)( const void *a , const void *b ) , void (*destructor)( void *ptr ) ) *\brief Add a pointer sorted in the list , starting by the end of the list. *\param list An initilized list container. A null value will cause an error and a _log message. *\param ptr The pointer you wan to put in the list. A null value will cause an error and a _log message. *\param comparator A pointer to a function which take two void * pointers and return an int. *\param destructor Pointer to the ptr type destructor function. Leave to NULL if there isn't. *\return TRUE or FALSE. Check error messages in DEBUG mode. */ int list_push_sorted( LIST *list , void *ptr , int (*comparator)( const void *a , const void *b ) , void (*destructor)( void *ptr ) ) { LIST_NODE *nodeptr = NULL ; n_assert( list , n_log( LOG_ERR , "invalid list: NULL" ); return FALSE ); n_assert( ptr , n_log( LOG_ERR , "invalid ptr: NULL" ); return FALSE ); if( list -> nb_max_items > 0 && ( list -> nb_items >= list -> nb_max_items ) ) { n_log( LOG_ERR , "list is full" ); return FALSE ; } if( list -> end ) { nodeptr = list -> end ; while( nodeptr && ( comparator( ptr , nodeptr -> ptr ) < 0 ) ) nodeptr = nodeptr -> prev ; if( !nodeptr ) { /* It's the lower ranked element in the sort */ list_unshift( list , ptr , destructor ); } else { /* we have a match inside the list. let's insert the datas */ LIST_NODE *node_next = nodeptr -> next ; LIST_NODE *newnode = new_list_node( ptr , destructor ); n_assert( newnode , n_log( LOG_ERR , "Couldn't allocate new node" ); return FALSE ); if( node_next ) { link_node( newnode , node_next ); } else list -> end = newnode ; link_node( nodeptr , newnode ); list -> nb_items ++ ; } }
/*在单链表头部添加元素*/ void slist_push_front(slist_t *l,void *item) { list_node_t *node=new_list_node(item); node->next=l->head; l->head=node; //无需区分头指针是否为空,情形一样 l->n++; }