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.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) (limited to 'linked_list.cpp') diff --git a/linked_list.cpp b/linked_list.cpp index bd49510e..d4bd5a91 100644 --- a/linked_list.cpp +++ b/linked_list.cpp @@ -3,27 +3,27 @@ template llist::llist () { this->length = 0; - this->cur = this->head = new link; - this->tail = new link; + this->cur = this->head = new llink; + this->tail = new llink; head->after = tail; tail->before = head; } template -link::~link () { +llink::~llink () { this.unlink(); } template -void link::unlink () { +void llink::unlink () { if (this->before != nullptr) this->before->after = this->after; if (this->after != nullptr) this->after->before = this->before; } template -void llist::push(T& val) { - auto l = new link(val); +void llist::push(T* val) { + auto l = new llink(val); l->after = FIRST(this); FIRST(this) = l; @@ -36,3 +36,52 @@ void llist::push(T& val) { // TODO do I want to change that? this->cur = l; } + +template +T& llist::peek() { + if (this->empty()) return nullptr; + + return FIRST(this)->value; +} + +template +T& llist::pop() { + if (this->empty()) return nullptr; + + llink* frst = FIRST(this); + frst.unlinke(); + + T& retval = frst->value; + --this->length; + delete frst; + + return retval; +} + +template +void llist::operator+= (llist& other) { + + /* Link end of dest onto start of new__. */ + LAST(this)->after = FIRST(other); + FIRST(other)->before = LAST(this); + + /* Free the two now not needed end links. */ + // free(other->head); + // free(other->tail); + delete other->head; + delete other->tail; + + /* Update dest with new__ tail ptr. */ + this->tail = other->tail; + + this->length += other->length; +} + +// template +// std::ostream& std::operator<<(std::ostream&, llist) { +// o << '('; +// for (T t : list) { +// o << t; +// } +// o << ')'; +// } -- cgit v1.2.3