summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/dlist/dlist-3.c
diff options
context:
space:
mode:
Diffstat (limited to 'rushs/tinyprintf/dlist/dlist-3.c')
-rw-r--r--rushs/tinyprintf/dlist/dlist-3.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/rushs/tinyprintf/dlist/dlist-3.c b/rushs/tinyprintf/dlist/dlist-3.c
new file mode 100644
index 0000000..22b4b52
--- /dev/null
+++ b/rushs/tinyprintf/dlist/dlist-3.c
@@ -0,0 +1,83 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "dlist.h"
+
+void dlist_map_square(struct dlist *list)
+{
+ for (struct dlist_item *i = list->head; i; i = i->next)
+ {
+ i->data *= i->data;
+ }
+}
+
+void dlist_reverse(struct dlist *list)
+{
+ struct dlist_item *tmp;
+ for (struct dlist_item *i = list->head; i; i = i->prev)
+ {
+ tmp = i->next;
+ i->next = i->prev;
+ i->prev = tmp;
+ }
+ tmp = list->tail;
+ list->tail = list->head;
+ list->head = tmp;
+}
+
+struct dlist *dlist_split_at(struct dlist *list, size_t index)
+{
+ if (list->size == 0)
+ return dlist_init();
+ if (index >= list->size)
+ {
+ return NULL;
+ }
+
+ struct dlist *new = dlist_init();
+ if (new == NULL)
+ return NULL;
+ new->size = list->size - index;
+ list->size = index;
+
+ struct dlist_item *i;
+ for (i = list->head; index; index--, i = i->next)
+ {
+ continue;
+ }
+
+ new->head = i;
+ new->tail = list->tail;
+ list->tail = i->prev;
+ if (i->prev)
+ {
+ i->prev->next = NULL;
+ }
+ else
+ {
+ list->head = NULL;
+ }
+ i->prev = NULL;
+ return new;
+}
+
+void dlist_concat(struct dlist *list1, struct dlist *list2)
+{
+ if (list2->head == NULL)
+ return;
+ if (list1->tail == NULL)
+ {
+ list1->head = list2->head;
+ list1->tail = list2->tail;
+ list1->size = list2->size;
+ return;
+ }
+
+ list1->tail->next = list2->head;
+ list2->head->prev = list1->tail;
+ list1->tail = list2->tail;
+ list1->size += list2->size;
+ list2->size = 0;
+ list2->head = NULL;
+ list2->tail = NULL;
+}