aboutsummaryrefslogtreecommitdiff
path: root/linked_list.inc.h
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 23:17:08 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 23:17:08 +0100
commita514490dcbb4aaaca3a5e4fc71ef194fea18b37f (patch)
tree16b8c4923fafbbe9e6e43e88beff08eb3f0674ae /linked_list.inc.h
parentAdd support for NL without preceeding CR. (diff)
downloadcalp-a514490dcbb4aaaca3a5e4fc71ef194fea18b37f.tar.gz
calp-a514490dcbb4aaaca3a5e4fc71ef194fea18b37f.tar.xz
Add llist FIRST & LAST, rewrote code to use them.
Diffstat (limited to 'linked_list.inc.h')
-rw-r--r--linked_list.inc.h31
1 files changed, 18 insertions, 13 deletions
diff --git a/linked_list.inc.h b/linked_list.inc.h
index 2622eb4b..1522a685 100644
--- a/linked_list.inc.h
+++ b/linked_list.inc.h
@@ -56,10 +56,12 @@ INIT_F ( LINK(TYPE), TYPE* val ) {
int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) {
NEW(LINK(TYPE), link, val);
- link->after = lst->head->after;
- lst->head->after = link;
+ link->after = FIRST(lst);
+ FIRST(lst) = link;
+
link->after->before = link;
- link->before = lst->head;
+ link->before = lst->head;
+
++lst->length;
// TODO do I want to change that?
@@ -69,7 +71,7 @@ int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) {
}
int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
- LINK(TYPE)* n = src->head->after;
+ LINK(TYPE)* n = FIRST(src);
while (n->after != NULL) {
NEW(TYPE, cpy);
@@ -89,22 +91,25 @@ int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
*/
int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new ) {
- LINK(TYPE)* og_new_head = new->head;
- dest->tail->before->after = new->head->after;
- new->head->after->before = dest->tail->before;
- LINK(TYPE)* old_tail = dest->tail;
+ /* Link end of dest onto start of new. */
+ LAST(dest)->after = FIRST(new);
+ FIRST(new)->before = LAST(dest);
+
+ /* Free the two now not needed end links. */
+ free(new->head);
+ free(dest->tail);
+
+ /* Update dest with new tail ptr. */
dest->tail = new->tail;
- dest->length += new->length;
- free(old_tail);
- free(og_new_head);
+ dest->length += new->length;
return 0;
}
int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
int size = 0;
- LINK(TYPE)* l = llist->head->after;
+ LINK(TYPE)* l = FIRST(llist);
while (l->after != NULL) {
++size;
}
@@ -112,7 +117,7 @@ int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
}
int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
- return llist->head->after == llist->tail;
+ return FIRST(llist) == llist->tail;
}
#endif /* TYPE */