Add a value to the list
This commit is contained in:
parent
ee7886b3a4
commit
242b07a949
|
@ -17,3 +17,42 @@ void ordinary_list_del(struct ordinary_list *list) {
|
|||
at = next;
|
||||
}
|
||||
}
|
||||
|
||||
static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_node *node) {
|
||||
if(!list->head) {
|
||||
list->head = node;
|
||||
list->tail = node;
|
||||
} else if(list->head == list->tail) {
|
||||
list->tail = node;
|
||||
list->head->next = list->tail;
|
||||
list->tail->prev = list->head;
|
||||
} else {
|
||||
list->tail->next = node;
|
||||
node->prev = list->tail;
|
||||
list->tail = node;
|
||||
}
|
||||
|
||||
if(list->limit) {
|
||||
list->count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) {
|
||||
if(list->limit && list->count == list->limit) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ordinary_node *node = malloc(sizeof(struct ordinary_node));
|
||||
|
||||
if(!node) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->prev = NULL;
|
||||
node->next = NULL;
|
||||
node->val = val;
|
||||
|
||||
ordinary_list_insert(list, node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue