From ecd15e3f6731a8298e9cd3ebc45e1875e8c19063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 19 Feb 2019 10:05:45 +0100 Subject: Now it links, but segfaults. --- linked_list.h | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'linked_list.h') diff --git a/linked_list.h b/linked_list.h index a27d82a1..7ed4c6c4 100644 --- a/linked_list.h +++ b/linked_list.h @@ -8,22 +8,20 @@ struct llink { llink* after = nullptr; T* value; - llink (); + llink () { }; llink (T* val) : value(val) { } ~llink (); void unlink (); }; -// #define L(link) (link)->value - template struct llist { llink* head; llink* tail; llink* __cur; T* cur() { return __cur->value; } - int length; + int length = 0; llist (); @@ -43,14 +41,40 @@ struct llist { bool empty () { return length == 0; } }; -// template -// std::ostream& std::operator<<(std::ostream&, llist); +template +llist::llist () { + this->__cur = this->head = new llink; + this->tail = new llink; + + head->after = tail; + tail->before = head; +} + #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 +template +void llist::push(T* val) { + auto l = new llink(val); + + l->after = FIRST(this); + FIRST(this) = l; + + l->after->before = l; + l->before = this->head; + + ++this->length; + + // TODO do I want to change that? + this->__cur = l; +} + +// template +// std::ostream& std::operator<<(std::ostream&, llist); + #if 0 DEEP_COPY LINK(TYPE)* n = FIRST(src); -- cgit v1.2.3