diff options
Diffstat (limited to 'linked_list.h')
-rw-r--r-- | linked_list.h | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/linked_list.h b/linked_list.h index 75096a93..1d95de80 100644 --- a/linked_list.h +++ b/linked_list.h @@ -10,7 +10,7 @@ struct llink { llink () { }; llink (T* val) : value(val) { } - ~llink (); + ~llink () { this->unlink(); } void unlink (); }; @@ -76,7 +76,7 @@ void llist<T>::push(T* val) { template <typename T> void llist<T>::reset () { - llink<T> *link = this->first, *next; + llink<T> *link = this->head, *next; while (link != this->tail) { next = link->after; @@ -86,6 +86,49 @@ void llist<T>::reset () { this->__cur = this->head; } +template <typename T> +void llink<T>::unlink () { + if (this->before != nullptr) this->before->after = this->after; + if (this->after != nullptr) this->after->before = this->before; +} + +template <typename T> +T& llist<T>::peek() { + if (this->empty()) return nullptr; + + return FIRST(this)->value; +} + +template <typename T> +T& llist<T>::pop() { + if (this->empty()) return nullptr; + + llink<T>* frst = FIRST(this); + frst.unlinke(); + + T& retval = frst->value; + --this->length; + delete frst; + + return retval; +} + +template <typename T> +void llist<T>::operator+= (llist<T>& 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. */ + delete other->head; + delete other->tail; + + /* Update dest with new__ tail ptr. */ + this->tail = other->tail; + + this->length += other->length; +} // template <typename T> // std::ostream& std::operator<<(std::ostream&, llist<T>); |