From 3362e2fb538c9426635e2fda8d9737662b48fc06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 19 Feb 2019 01:25:45 +0100 Subject: I somehow got stuff to bulid, now it just doesn't link. --- linked_list.h | 82 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 42 deletions(-) (limited to 'linked_list.h') diff --git a/linked_list.h b/linked_list.h index 7d11ca81..3eab5549 100644 --- a/linked_list.h +++ b/linked_list.h @@ -3,14 +3,14 @@ template -struct link { - link* before = nullptr; - link* after = nullptr; +struct llink { + llink* before = nullptr; + llink* after = nullptr; T* value; - link (); - link (T* val) : value(val) { } - ~link (); + llink (); + llink (T* val) : value(val) { } + ~llink (); void unlink (); }; @@ -19,64 +19,62 @@ struct link { template struct llist { - link* head; - link* tail; - link* cur; + llink* head; + llink* tail; + llink* cur; int length; llist (); - void push ( T& ); + void push ( T* ); T& peek (); T& pop (); - llist& operator += (llist& other); + /* + * Adds two linked lists together. + * O(1) time. + * destroys new__ in the process, but keeps the elements. + * make sure to free(new__) after. + */ + void operator += (llist& other); + + int size () { return length; } + bool empty () { return length == 0; } }; +// template +// std::ostream& std::operator<<(std::ostream&, llist); + #define FIRST(lst) (lst)->head->after #define FIRST_V(lst) (lst)->head->after->value #define LAST(lst) (lst)->tail->before #define LAST_V(lst) (lst)->tail->before->value -/* - * NOTE freeing a linked list alsa FFREE's all its contents. - * TODO some form of shared pointer to ensure nothing is free'd twice - * would be a good idea. - */ - -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); +#if 0 +DEEP_COPY + LINK(TYPE)* n = FIRST(src); -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new_ ); + while (n->after != NULL) { + NEW(TYPE, cpy); + DEEP_COPY(TYPE)(cpy, n->value); + PUSH(LLIST(TYPE)) ( dest, cpy ); + n = n->after; + } -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ); -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); + return 0; +#endif /* * Resets a linked list by removing all it's objects. * FREE's all elements stored in the list. */ -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +// int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +#if 0 LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); - -FMT_F(LLIST(TYPE)); - -/* Iterator */ - -#define __PRE_LLIST(T, l, set) \ - T* l; LINK(T)* __INTER(l); - -#define PRE_FOR_LLIST(T) __PRE_LLIST - -// #define __BEG_LLIST(v, set) v = (set)->head -#define __BEG_LLIST(T, l, set) __INTER(l) = FIRST(set), l = L(__INTER(l)) -#define BEG_LLIST(T) __BEG_LLIST - -#define __END_LLIST(T, l, set) __INTER(l) != (set)->tail -#define END_LLIST(T) __END_LLIST - -#define __NXT_LLIST(T, l, set) __INTER(l) = __INTER(l)->after, l = L(__INTER(l)) -// #define __NXT_LLIST(T, l, set) l = L(__INTER(l) = __INTER(l)->after) -#define NXT_LLIST(T) __NXT_LLIST + if (dest == NULL) return new__; + APPEND(LLIST(TYPE))(dest, new__); + return dest; +#endif #endif /* LINKED_LIST_H */ -- cgit v1.2.3