|
@@ -0,0 +1,65 @@
|
|
|
+#include <iostream>
|
|
|
+#include "Node.h"
|
|
|
+
|
|
|
+
|
|
|
+class Chain
|
|
|
+{
|
|
|
+
|
|
|
+ public:
|
|
|
+ void* start;
|
|
|
+ void* end;
|
|
|
+
|
|
|
+ Chain()
|
|
|
+ {
|
|
|
+ start = nullptr;
|
|
|
+ end = nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool isNull() const
|
|
|
+ {
|
|
|
+ return start == nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ template <typename T>
|
|
|
+ void add(T value)
|
|
|
+ {
|
|
|
+ if(start==nullptr) {
|
|
|
+ Node<T> tstart(value);
|
|
|
+ start = &tstart;
|
|
|
+ end = &tstart;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Node<T> node(value, static_cast<Node<T>*>(end));
|
|
|
+ static_cast<Node<T>*>(end)->next = &node;
|
|
|
+ end = &node;
|
|
|
+ }
|
|
|
+
|
|
|
+ template <typename T>
|
|
|
+ Node<T> pop()
|
|
|
+ {
|
|
|
+ T value = static_cast<Node<T>*>(end)->value;
|
|
|
+ if(static_cast<Node<T>*>(end)->prev == nullptr)
|
|
|
+ {
|
|
|
+ start = nullptr;
|
|
|
+ end = nullptr;
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ end = static_cast<Node<T>*>(end)->prev;
|
|
|
+ static_cast<Node<T>*>(end)->next = nullptr;
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ template <typename T>
|
|
|
+ friend std::ostream &operator <<(std::ostream &os, Chain &c)
|
|
|
+ {
|
|
|
+ os << "( ";
|
|
|
+ void* currentNode = c.start;
|
|
|
+ while (static_cast<Node<T>*>(currentNode)->next!=nullptr)
|
|
|
+ {
|
|
|
+ os << *(static_cast<Node<T>*>(currentNode)) << " = ";
|
|
|
+ currentNode = static_cast<Node<T>*>(currentNode)->next;
|
|
|
+ }
|
|
|
+ os << *(static_cast<Node<T>*>(currentNode)) << " )";
|
|
|
+ return os;
|
|
|
+ }
|
|
|
+};
|