http://kmlinux.fjfi.cvut.cz/~culik/zpro/tree #ifndef TREE_H #define TREE_H #include using std::string; class Node { private: Node*up; Node*next; Node*prev; Node*first; Node*last; void link (Node*a,Node*b, Node*c); //b novy prvek public: string name; Node*getUp(){return up;} Node*getNext(){return next;} Node*getPrev(){return prev;} Node*getFirst(){return first;} Node*getLast(){return last;} void linkNext (Node*p); void linkPrev (Node*p); void linkFirst (Node*p); void linkLast (Node*p); void unlink (); void print (int level = 1); Node(string name0=""): up(nullptr),next(nullptr), prev(nullptr), first(nullptr), last(nullptr), name(name0) {} ~Node(); }; #endif // TREE_H #include "tree.h" #include #include using namespace std; void Node::link(Node *a, Node *b, Node *c) { // #include assert (this != nullptr); //nemelo by se stat, ale pro jistotu assert (b != nullptr); assert (b->up == nullptr); b->up = this; assert (a == nullptr || a->up == this); assert (c == nullptr || c->up == this); b->prev = a; b->next = c; if (a != nullptr) a->next = b; else first = b; if (c != nullptr) c->prev = b; else last = b; } void Node::linkFirst(Node *p) { assert(this != nullptr); link (nullptr, p, first); } void Node::linkLast(Node *p) { assert(this != nullptr); link (last, p, nullptr); } void Node::linkPrev(Node *p) { assert(this != nullptr); assert (up!=nullptr); up->link (prev, p, this); } void Node::linkNext(Node *p) { assert(this != nullptr); assert (up!=nullptr); up->link (this, p, next); } void Node::unlink() { assert (this != nullptr); assert (up != nullptr); Node * a = prev; Node * c = next; if (a != nullptr) a->next = c; // mame predchudce, muzeme propojit else up->first = c; // nemame predchudce, naslednik se stava prvnim if (c != nullptr) c->prev = a; else up->last = a; up = nullptr; prev = nullptr; next = nullptr; } Node::~Node() { cout << "Mažu " << name << endl; if (up != nullptr) unlink(); Node * p = first; while (p != nullptr) { Node * t = p->next; delete p; p = t; } } void Node::print (int level) { cout << string (level*4,' ') << name << endl; if (first != nullptr) { cout << string (level*4 + 2, ' ') << "[" << endl; Node * p = first; while (p != nullptr) { p->print (level + 1); p = p->next; } cout << string (level*4 + 2,' ') << "]" << endl; } }