===== Struktury ===== #include using namespace std; struct Item { string name; int r, g, b; }; void print (Item & t) { cout << t.name << " " << t.r << "," << t.g << "," << t.b << endl; } int main() { Item c; c.name = "cervena"; c.r = 255; c.g = 0; c.b = 0; Item m; m.name = "modra"; m.r = 0; m.g = 0; m.b = 255; print (c); print (m); return 0; } ===== Funkce swap s parametry předávanými odkazem ======= #include using namespace std; void swap0 (int x, int y) // nefunguje { int t = x; x = y; y = t; cout << "(" << x << ", " << y << ")"<< endl; } inline void swap (int & x, int & y) { int t = x; x = y; y = t; } int main() { int a = 1; int b = 2; cout << a << ", " << b << endl; swap (a, b); cout << a << ", " << b << endl; int p[10]; swap (p[0], p[1]); return 0; } ===== Ukazatele ===== #include using namespace std; struct Item { string name; int r, g, b; Item* next; }; Item* first = nullptr; // prazdny seznam void print(Item* t) { cout << t->name << " " << t->r << "," << t->g << "," << t->b << endl; } int main() { Item* c = new Item; c->name = "cervena"; c->r = 255; c->g = 0; c->b = 0; Item* z = new Item; z->name = "zelena"; z->r = 0; z->g = 255; z->b = 0; Item* m = new Item; m->name = "modra"; m->r = 0; m->g = 0; m->b = 255; // Item * u = z; // u->name = "svetle zelena"; // u->r += 128; // u->b += 128; first = c; c->next = z; // za cervenou bude zelena z->next = m; m->next = nullptr; // za modrou nebude nic // first->next = z; // first->next->next = m; // first->next->next->next = nullptr; // print (first); // print (first->next); // print (first->next->next); Item* u = first; while (u != nullptr) { print(u); u = u->next; } return 0; } ===== Vkládání do seznamu ===== #include using namespace std; struct Item { string name; int r, g, b; Item * next; }; Item * first = nullptr; void insertFirst (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->next = first; first = t; } void insertLast (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->next = nullptr; // nikdo za mnou nebude if (first == nullptr) { first = t; } else { Item * p = first; while (p->next != nullptr) { p = p->next; } p->next = t; // pripojime za posledni } } Item * find (string name0) { Item * p = first; while (p != nullptr && p->name != name0) { p = p->next; } return p; } void print () { Item * t = first; while (t != nullptr) { cout << t->name << " " << t->r << "," << t->g << "," << t->b << endl; t = t->next; } } int main() { insertLast ("cervena", 255, 0, 0); insertLast ("zelena", 0, 255, 0); insertLast ("modra", 0, 0, 255); Item * t = find ("zelena"); if (t == nullptr) cout << "nenasli" << endl; else cout << "nasli " << t->name << ", " << t->r << ", " << t->g << ", " << t->b << endl; print (); return 0; } {{seznam.png}} * cervena policka jsou typu //Item *// (ukazatel na strukturu Item) * zluta jsou typu //string// * modra typu //int// Nakresleno pomocí https://app.diagrams.net/ ===== Vymazávamí ze seznam ===== #include #include using namespace std; struct Item { string name; int r, g, b; Item * next; }; Item * first = nullptr; void insertFirst (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->next = first; first = t; } void insertLast (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->next = nullptr; // nikdo za mnou nebude if (first == nullptr) { first = t; } else { Item * p = first; while (p->next != nullptr) { p = p->next; } p->next = t; // pripojime za posledni } } void insertAfter (Item * target, string name0, int r0, int g0, int b0) // vlozit novy prvek za target { assert (target != nullptr); Item* t = new Item; t->name = name0; t->r = r0; t->g = g0; t->b = b0; t->next = target->next; // za novym prvkem bude naslednik prvku target target->next = t; // za target bude novy prvek } void remove (Item* target) // odstranit prvek target { assert (target != nullptr); if (target == first) { first = target->next; } else { Item* p = first; while (p != nullptr && p->next != target) { p = p->next; } assert (p != nullptr); p->next = target->next; } delete target; } Item* find (string name0) { Item* p = first; while (p != nullptr && p->name != name0) { p = p->next; } return p; } void print () { Item* t = first; while (t != nullptr) { cout << t->name << " " << t->r << "," << t->g << "," << t->b << endl; t = t->next; } } /* void light (int & h) { h = h + 128; if (h > 255) h = 255; } */ int main() { insertLast ("cervena", 255, 0, 0); insertLast ("zelena", 0, 255, 0); insertLast ("modra", 0, 0, 255); Item* t = find ("zelena"); if (t == nullptr) cout << "nenasli" << endl; else { cout << "nasli " << t->name << ", " << t->r << ", " << t->g << ", " << t->b << endl; insertAfter (t, "zluta", 192, 192, 0); remove (t); /* t->name = "svetle " + t->name; light (t->r); light (t->g); light (t->b); */ } cout << endl; print(); while (first != nullptr) { remove (first); } print(); return 0; }