#include "stdafx.h" #include #include #include #include using namespace std; // class List; class T { public: string data; int r, g, b; T* prev; T* next; // List * owner; T (); T (string jmeno, int r0, int g0, int b0); ~T () { } }; T::T () : data (""), r (0), g (0), b (0), prev (nullptr), next (nullptr) { } T::T (string jmeno, int r0, int g0, int b0) : data (jmeno), r (r0), g (g0), b (b0), prev (nullptr), next (nullptr) { } class List { public: T* first; T* last; List (); ~List (); T * search (string name); void place (T* prvek); void placeTo (T* prvek, T* ref); void smaz (T* prvek); // void move (List & source); void Check (); void print (); }; List::~List () { T* p = first; while (p != nullptr) { T*a = p->next; smaz (p); delete p; p = a; } // first = nullptr; // last = nullptr; } void List::smaz (T* prvek) { T*a = prvek->prev; T*b = prvek->next; if (a != nullptr) { a->next = b; } else { first = b; } if (b != nullptr) { b->prev = a; } else { last = a; } prvek->next = nullptr; prvek->prev = nullptr; } T * List::search (string name) { T * p = first; while (p != nullptr && p->data != name) p = p->next; /* bool found = false; while (p != nullptr && ! found) { if (p->data == name) { found = true; } if (!found) { p = p->next; } } */ /* while (p != nullptr) { if (p->data == name) { break; } p = p->next; } */ return p; } List::List () : first (nullptr), last (nullptr) { } void List::place (T* prvek) { if (first == nullptr) { last = prvek; first = prvek; prvek->prev = nullptr; prvek->next = nullptr; } else { last->next = prvek; prvek->prev = last; last = prvek; prvek->next = nullptr; } } void List::placeTo (T* prvek, T* ref) { prvek->prev = ref; prvek->next = ref->next; if (ref->next != nullptr) { ref->next->prev = prvek; } else { last = prvek; } ref->next = prvek; } void List::Check () { if (first == nullptr ) { assert (last == nullptr); } else { assert (last != nullptr); T* prv = first; int count = 0; while (prv != nullptr) { count++; prv = prv->next; } prv = last; while (prv != NULL) { count--; prv = prv->prev; } assert (count == 0); } } void List::print () { T* prv = first; while (prv != nullptr) { cout << prv->data << endl; prv = prv->next; } } int main() { cout << "Zacatek" << endl; List a; a.Check (); a.place (new T ("neco", 128, 128, 128)); a.Check (); a.place (new T ("neco2", 128, 128, 128)); a.Check (); a.placeTo (new T ("neco3", 128, 128, 128), a.first); a.Check (); a.placeTo (new T ("neco4", 128, 128, 128), a.last); a.Check (); a.print (); a.smaz (a.first->next); a.Check (); a.smaz (a.last); a.Check (); a.smaz (a.first); a.Check (); cout << endl; a.print(); system ("pause"); return 0; }