====== Dvousměrný seznam ====== #include #include using namespace std; struct Item; struct List { Item * first; Item * last; }; struct Item { string name; int r, g, b; Item * prev; Item * next; }; void init (List & a) { a.first = nullptr; a.last = nullptr; } void link (List& a, Item* bef, Item* cur, Item* aft) // Pripojit novy prvek // a - seznam // bef - predchazejici prvek (muze byt nullptr) // cur - novy prvek // bef - nasledujici prvek (muze byt nullptr) { cur->prev = bef; cur->next = aft; if (bef != nullptr) bef->next = cur; else a.first = cur; if (aft != nullptr) aft->prev = cur; else a.last = cur; } inline void linkFirst (List & a, Item * p) // Pripojit novy prvek p na zacatek seznamu // Novy prvek nesmi byt soucasti zadneho seznamu { link (a, nullptr, p, a.first); } inline void linkLast (List & a, Item * p) { link (a, a.last, p, nullptr); } void insertFirst (List& a, string name0, int r0, int g0, int b0) { Item* t = new Item; t->name = name0; t->r = r0; t->g = g0; t->b = b0; // t->prev = nullptr; // t->next = nullptr; linkFirst (a, t); } void insertLast (List& a, string name0, int r0, int g0, int b0) { Item* t = new Item; t->name = name0; t->r = r0; t->g = g0; t->b = b0; linkLast (a, t); } void linkAfter (List& a, Item* ref, Item* fresh) // Pripojit novy prvek fresh za prvej ref // Prvek ref musi byt soucasti seznamu a // Novy prvek fresh nesmi byt soucasti zadneho seznamu { assert(ref != nullptr); link (a, ref, fresh, ref->next); } void linkBefore (List& a, Item* ref, Item* fresh) { assert (ref != nullptr); link (a, ref->prev, fresh, ref); } void insertAfter (List& a, Item* ref, string name0, int r0, int g0, int b0) { Item* t = new Item; t->name = name0; t->r = r0; t->g = g0; t->b = b0; linkAfter (a, ref, t); } void insertBefore (List& a, Item* ref, string name0, int r0, int g0, int b0) { Item* t = new Item; t->name = name0; t->r = r0; t->g = g0; t->b = b0; linkBefore (a, ref, t); } Item* find (List& a, string name0) { Item* p = a.first; while (p != nullptr && p->name != name0) { p = p->next; } return p; } void unlink (List& a, Item* cur) // Odpojit prvek cur ze seznamu a // (Prvek cur musi byt soucasti seznamu a) { assert (cur != nullptr); Item* bef = cur->prev; Item* aft = cur->next; if (bef != nullptr) bef->next = aft; else a.first = aft; if (aft != nullptr) aft->prev = bef; else a.last = bef; cur->prev = nullptr; cur->next = nullptr; } void remove (List& a, Item* p) { unlink (a, p); delete p; } void clear (List& a) { Item* p = a.first; while (p != nullptr) { Item * t = p->next; delete p; p = t; } a.first = nullptr; a.last = nullptr; } void print (List& a) { Item* t = a.first; while (t != nullptr) { cout << t->name << " " << t->r << "," << t->g << "," << t->b << endl; t = t->next; } } int main() { List b; init(b); insertLast (b, "cervena", 255, 0, 0); insertLast (b, "zelena", 0, 255, 0); insertLast (b, "modra", 0, 0, 255); Item* t = find(b, "zelena"); if (t == nullptr) cout << "nenasli" << endl; else { cout << "nasli " << t->name << ", " << t->r << ", " << t->g << ", " << t->b << endl; insertAfter (b, t, "svetle " + t->name, 128, t->g, 128); } cout << endl; print (b); List c; init (c); while (b.last != NULL) { Item * t = b.last; unlink (b, t); linkLast (c, t); } cout << endl; print(c); clear(c); return 0; }