#include "list.h" // #include #include #include using namespace std; List::List() : first (nullptr), last (nullptr) { } Item::Item (string name0) : name (name0), owner (nullptr), prev (nullptr), next (nullptr) { } void List::insertFirst (string name0) { linkFirst ( new Item (name0) ); } void List::insertLast (string name0) { // Item * p = new Item; // p->name = name0; Item * p = new Item (name0); linkLast (p); } void List::linkFirst(Item *p) { link (nullptr, p, first); } void List::linkLast(Item *p) { link (last, p, nullptr); } void List::linkBefore (Item *old, Item *p) // vloz p pred old { assert (old != nullptr); link (old->prev, p, old); } void List::linkAfter(Item *old, Item *p) // vloz p za old { assert (old != nullptr); link (old, p, old->next); } void List::link(Item *a, Item *b, Item *c) { // #include assert (b != nullptr); assert (b->owner == nullptr); b->owner = this; assert (a == nullptr || a->owner == this); assert (c == nullptr || c->owner == 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 List::unlink (Item *p) { assert (p != nullptr); assert (p->owner == this); Item * a = p->prev; Item * c = p->next; if (a != nullptr) a->next = c; // mame predchudce, muzeme propojit else first = c; // nemame predchudce, naslednik se stava prvnim if (c != nullptr) c->prev = a; else last = a; p->owner = nullptr; p->prev = nullptr; p->next = nullptr; } List::~List() { Item * p = first; while (p != nullptr) { Item * t = p->next; unlink (p); // cout << "Mazeme " << p->name << endl; delete p; p = t; } } void List::move (List & target) { Item * p = first; while (p != nullptr) { Item * t = p->next; unlink (p); target.linkLast (p); p = t; } } void List::transfer (List & source) { Item * p = source.first; while (p != nullptr) { Item * t = p->next; source.unlink (p); linkLast (p); p = t; } } void List::reverse () { List target; Item * p = first; while (p != nullptr) { Item * t = p->next; unlink (p); target.linkFirst (p); p = t; } target.move (*this); } void Indent(int level) { if (level > 0) cout << string(level * 4,' '); } void List::print (int level) { // #include Indent(level); cout << "[" << endl; Item * p = first; while (p != nullptr) { Indent(level); cout << " " << p->name << " " << p->info << endl; if (p->inner.first != nullptr) p->inner.print(level + 1); p = p->next; } Indent(level); cout << "]" << endl; } void List::insert (string name0) { Item * t = first; while (t != nullptr && t->name <= name0) t = t->next; Item * p = new Item (name0); if (t != nullptr) linkBefore (t, p); else linkLast (p); }