1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #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;
- }
- };
|