// add new list node to list head static inline void list_add_head(PT_LIST_LINKNODE ptListHead, PT_LIST_LINKNODE ptListNewLink) { PT_LIST_LINKNODE ptPrevLink = ptListHead; PT_LIST_LINKNODE ptNextLink = ptListHead->next; _list_add(ptListNewLink, ptPrevLink, ptNextLink); }
// add new list node to list tail static inline void list_add_tail(PT_LIST_LINKNODE ptListHead, PT_LIST_LINKNODE ptListNewLink) { PT_LIST_LINKNODE ptPrevLink = ptListHead->prev; PT_LIST_LINKNODE ptNextLink = ptListHead; _list_add(ptListNewLink, ptPrevLink, ptNextLink); }
struct int_list *radix_sort(struct int_list *arr) { int i,j, pos = 10; struct int_list *head, *next_element, *digits[10]; for(i = 0; i < 10; i++) { digits[i] = NULL; } for(j = 0; j < 10; j++) { /* Group Phase */ for(i = 0; i < ARR_SIZE; i++) { next_element = list_entry(arr->list.next, struct int_list, list); if(digits[(arr->val % pos) / (pos / 10)] == NULL) { digits[(arr->val % pos) / (pos / 10)] = arr; _list_del(arr); _list_add(arr, arr, arr); } else { _list_del(arr); _list_add(arr, list_entry(digits[(arr->val % pos) / (pos / 10)]->list.prev, struct int_list, list), digits[(arr->val % pos) / (pos / 10)]); } arr = next_element; } /* Merge phase */ head = arr = NULL; for(i = 0; i < 10; i++) { if(digits[i] != NULL) { if(head == NULL) head = digits[i]; if(arr != NULL) { struct list_head *ap = arr->list.prev; arr->list.prev->next = &digits[i]->list; /* a'->next */ arr->list.prev = digits[i]->list.prev; /* a->prev */ digits[i]->list.prev->next = &arr->list; /*d'->next */ digits[i]->list.prev = ap; /*d->prev */ } else { arr = head; /* This is here because I want to skip the first loop. */ } digits[i] = NULL; } } pos *= 10; } return head; }
/** * list_replace - replace node with new_node in a list. * 替换一个node * * @param[in] node old node * @param[in] new_node new node * * @return none * * @authors deeve * @date 2013/10/19 */ void list_replace(struct list_head *node, struct list_head *new_node) { struct list_head *next, *prev; if (node && new_node) { prev = node->prev; next = node->next; _list_add(new_node, prev, next); } }
/** * list_replace - replace node with new_node in a list, and reinitialize node. * 替换一个node,并且取消old node的链接关系 * * @param[in] node old node * @param[in] new_node new node * * @return none * * @authors deeve * @date 2013/10/19 */ void list_replace_init(struct list_head *node, struct list_head *new_node) { struct list_head *next, *prev; if (node && new_node) { prev = node->prev; next = node->next; _list_add(new_node, prev, next); INIT_LIST_HEAD(node); } }
static void _trash_list(Trash * trash) { Config * config; char * path; DIR * dir; struct dirent * de; time_t sixmonths; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif /* FIXME report errors */ if((path = _trash_get_path(DATA_TRASHINFO)) == NULL) return; if((config = config_new()) == NULL) { free(path); return; } /* FIXME should try to create the directory */ if((dir = opendir(path)) == NULL) { config_delete(config); free(path); return; } /* FIXME refresh only if necessary */ _list_reset(trash); sixmonths = time(NULL) - 15552000; while((de = readdir(dir)) != NULL) _list_add(trash, config, path, de->d_name, sixmonths); closedir(dir); _list_purge(trash); config_delete(config); free(path); }
/* Create an uninitialized element at the specified position */ static inline struct int_list *list_add(struct int_list *prev, struct int_list *next) { return _list_add(malloc(sizeof(struct int_list)), prev, next); }
/** * list_add_tail - add a new_node entry to the list tail. * Insert a new_node entry before the specified head * This is useful for implementing queues * * @param[in] new_node entry to be added * @param[in] head list head to add it before * * @return none * * @see * @authors deeve * @date 2013/10/19 */ void list_add_tail(struct list_head *new_node, struct list_head *head) { _check_head(head); _list_add(new_node, head->prev, head); }
/** * list_add - add a new entry. * Insert a new_node entry after the specified head * This is good for implementing stacks * * @param[in] new_node entry to be added * @param[in] head list head to add it after * * @return none * * @see * @authors deeve * @date 2013/10/19 */ void list_add(struct list_head *new_node, struct list_head *head) { _check_head(head); _list_add(new_node, head, head->next); }