aboutsummaryrefslogtreecommitdiff
path: root/linked_list.h
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-02-19 10:05:45 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-19 10:18:33 +0100
commitecd15e3f6731a8298e9cd3ebc45e1875e8c19063 (patch)
treed976a5fd710cc39350a37a2aa2f37fd08d0926f5 /linked_list.h
parentNo idea, to tired. (diff)
downloadcalp-ecd15e3f6731a8298e9cd3ebc45e1875e8c19063.tar.gz
calp-ecd15e3f6731a8298e9cd3ebc45e1875e8c19063.tar.xz
Now it links, but segfaults.
Diffstat (limited to 'linked_list.h')
-rw-r--r--linked_list.h36
1 files changed, 30 insertions, 6 deletions
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<T>* after = nullptr;
T* value;
- llink ();
+ llink () { };
llink (T* val) : value(val) { }
~llink ();
void unlink ();
};
-// #define L(link) (link)->value
-
template <typename T>
struct llist {
llink<T>* head;
llink<T>* tail;
llink<T>* __cur;
T* cur() { return __cur->value; }
- int length;
+ int length = 0;
llist ();
@@ -43,14 +41,40 @@ struct llist {
bool empty () { return length == 0; }
};
-// template <typename T>
-// std::ostream& std::operator<<(std::ostream&, llist<T>);
+template <typename T>
+llist<T>::llist () {
+ this->__cur = this->head = new llink<T>;
+ this->tail = new llink<T>;
+
+ 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 <typename T>
+void llist<T>::push(T* val) {
+ auto l = new llink<T>(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 <typename T>
+// std::ostream& std::operator<<(std::ostream&, llist<T>);
+
#if 0
DEEP_COPY
LINK(TYPE)* n = FIRST(src);