Compare commits
2 commits
26f425b811
...
2eeca32ac9
Author | SHA1 | Date | |
---|---|---|---|
2eeca32ac9 | |||
e07bb4eee0 |
4 changed files with 20 additions and 42 deletions
|
@ -13,6 +13,7 @@ typedef struct {
|
|||
size_t length;
|
||||
size_t element_size;
|
||||
Node *head;
|
||||
Node *tail;
|
||||
} LinkedList;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
@ -16,6 +16,7 @@ LinkedList linked_list_new(size_t element_size) {
|
|||
ret.length = 0;
|
||||
ret.element_size = element_size;
|
||||
ret.head = NULL;
|
||||
ret.tail = NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -24,17 +25,14 @@ void linked_list_insert(LinkedList *l, void *value) {
|
|||
if (l->head == NULL) {
|
||||
l->head = node_new(value, l->element_size);
|
||||
l->head->next = l->head;
|
||||
l->tail = l->head;
|
||||
l->length += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
Node *tail = l->head;
|
||||
while (tail->next != l->head) {
|
||||
tail = tail->next;
|
||||
}
|
||||
|
||||
tail->next = node_new(value, l->element_size);
|
||||
tail->next->next = l->head;
|
||||
l->tail->next = node_new(value, l->element_size);
|
||||
l->tail->next->next = l->head;
|
||||
l->tail = l->tail->next;
|
||||
l->length += 1;
|
||||
}
|
||||
|
||||
|
@ -42,19 +40,8 @@ LinkedList linked_list_merge(LinkedList l1, LinkedList l2) {
|
|||
if (l1.head == NULL) return l2;
|
||||
if (l2.head == NULL) return l1;
|
||||
|
||||
Node *l1_tail = l1.head;
|
||||
while (l1_tail->next != l1.head) {
|
||||
l1_tail = l1_tail->next;
|
||||
}
|
||||
|
||||
l1_tail->next = l2.head;
|
||||
|
||||
Node *l2_tail = l2.head;
|
||||
while (l2_tail->next != l2.head) {
|
||||
l2_tail = l2_tail->next;
|
||||
}
|
||||
|
||||
l2_tail->next = l1.head;
|
||||
l1.tail->next = l2.head;
|
||||
l2.tail->next = l1.head;
|
||||
|
||||
l1.length += l2.length;
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ typedef struct {
|
|||
size_t length;
|
||||
size_t element_size;
|
||||
Node *head;
|
||||
Node *tail;
|
||||
} DoublyLinkedList;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
@ -17,6 +17,7 @@ DoublyLinkedList doubly_linked_list_new(size_t element_size) {
|
|||
ret.length = 0;
|
||||
ret.element_size = element_size;
|
||||
ret.head = NULL;
|
||||
ret.tail = NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -26,20 +27,18 @@ void doubly_linked_list_insert(DoublyLinkedList *l, void *value) {
|
|||
l->head = node_new(value, l->element_size);
|
||||
l->head->next = l->head;
|
||||
l->head->prev = l->head;
|
||||
l->tail = l->head;
|
||||
l->length += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
Node *tail = l->head;
|
||||
while (tail->next != l->head) {
|
||||
tail = tail->next;
|
||||
}
|
||||
l->tail->next = node_new(value, l->element_size);
|
||||
l->tail->next->next = l->head;
|
||||
l->tail->next->prev = l->tail;
|
||||
|
||||
tail->next = node_new(value, l->element_size);
|
||||
tail->next->next = l->head;
|
||||
tail->next->prev = tail;
|
||||
l->tail = l->tail->next;
|
||||
|
||||
l->head->prev = tail->next;
|
||||
l->head->prev = l->tail;
|
||||
l->length += 1;
|
||||
}
|
||||
|
||||
|
@ -47,21 +46,11 @@ DoublyLinkedList doubly_linked_list_merge(DoublyLinkedList l1, DoublyLinkedList
|
|||
if (l1.head == NULL) return l2;
|
||||
if (l2.head == NULL) return l1;
|
||||
|
||||
Node *l1_tail = l1.head;
|
||||
while (l1_tail->next != l1.head) {
|
||||
l1_tail = l1_tail->next;
|
||||
}
|
||||
l2.head->prev = l1.tail;
|
||||
l1.tail->next = l2.head;
|
||||
|
||||
l2.head->prev = l1_tail;
|
||||
l1_tail->next = l2.head;
|
||||
|
||||
Node *l2_tail = l2.head;
|
||||
while (l2_tail->next != l2.head) {
|
||||
l2_tail = l2_tail->next;
|
||||
}
|
||||
|
||||
l1.head->prev = l2_tail;
|
||||
l2_tail->next = l1.head;
|
||||
l1.head->prev = l2.tail;
|
||||
l2.tail->next = l1.head;
|
||||
|
||||
l1.length += l2.length;
|
||||
|
||||
|
|
Loading…
Reference in a new issue