Objektový seznam (první část)

#include <iostream>
#include <cassert>
using namespace std;
 
class Item;
 
class List
{
public:
    Item* first;
    Item* last;
private:
    void link (Item* bef, Item* cur, Item* aft);
public:
    void linkFirst (Item* cur);
    void linkLast (Item* cur);
    void linkBefore (Item* target, Item* fresh);
    void linkAfter (Item* target, Item* fresh);
 
    void insertFirst (string name0, int r0, int g0, int b0);
    void insertLast (string name0, int r0, int g0, int b0);
    void insertBefore (Item* target, string name0, int r0, int g0, int b0);
    void insertAfter (Item* target, string name0, int r0, int g0, int b0);
 
    void unlink (Item* cur);
    void remove (Item* cur);
 
    Item* find (string name0);
    void print ();
 
    List ();
    ~List ();
};
 
class Item
{
public:
    string name;
    int    r, g, b;
    Item * prev;
    Item * next;
    Item ();
    Item (string name0, int r0 = 255, int g0 = 255, int b0 = 255);
};
 
Item::Item() : name(""), r(255), g(255), b(255), prev(nullptr), next(nullptr) { }
Item::Item(string name0, int r0, int g0, int b0) : name(name0), r(r0), g(g0), b(b0), prev(nullptr), next(nullptr) { }
 
List::List ()
{
    first = nullptr;
    last = nullptr;
}
 
List :: ~ List ()
{
    Item* p = first;
    while (p != nullptr)
    {
        Item * t = p->next;
        delete p;
        p = t;
    }
}
 
void List :: link (Item* bef, Item* cur, Item* aft)
{
    cur->prev = bef;
    cur->next = aft;
 
    if (bef != nullptr)
        bef->next = cur;
    else
        this->first = cur;
 
    if (aft != nullptr)
        aft->prev = cur;
    else
        last = cur;
}
 
void List :: linkFirst (Item * cur)
{
    this->link (nullptr, cur, this->first);
}
 
void List :: linkLast (Item * cur)
{
    link (last, cur, nullptr);
}
 
void List :: linkAfter (Item* target, Item* fresh)
{
    assert (target != nullptr);
    link (target, fresh, target->next);
}
 
void List :: linkBefore (Item* target, Item* fresh)
{
    assert (target != nullptr);
    link (target->prev, fresh, target);
}
 
void List :: insertFirst (string name0, int r0, int g0, int b0)
{
    linkFirst (new Item (name0, r0, g0, b0));
}
 
void List :: insertLast (string name0, int r0, int g0, int b0)
{
    linkLast (new Item(name0, r0, g0, b0));
}
 
void List :: insertBefore (Item* target, string name0, int r0, int g0, int b0)
{
    linkBefore (target, new Item(name0, r0, g0, b0));
}
 
void List :: insertAfter (Item* target, string name0, int r0, int g0, int b0)
{
    linkAfter (target, new Item(name0, r0, g0, b0));
}
 
void List::unlink (Item* cur)
{
    assert (cur != nullptr);
 
    Item* bef = cur->prev;
    Item* aft = cur->next;
 
    if (bef != nullptr)
        bef->next = aft;
    else
        first = aft;
 
    if (aft != nullptr)
        aft->prev = bef;
    else
        last = bef;
 
    cur->prev = nullptr;
    cur->next = nullptr;
}
 
void List::remove (Item* cur)
{
    unlink (cur);
    delete cur;
}
 
Item* List::find (string name0)
{
    Item* p = first;
    while (p != nullptr && p->name != name0)
    {
        p = p->next;
    }
    return p;
}
 
void List::print ()
{
    Item* t = first;
    while (t != nullptr)
    {
        cout << t->name << " " << t->r << "," << t->g << "," << t->b << endl;
        t = t->next;
    }
}
 
int main()
{
    List b;
 
    b.insertLast ("cervena", 255, 0, 0);
    b.insertLast ("zelena", 0, 255, 0);
    b.insertLast ("modra", 0, 0, 255);
 
    Item* t = b.find ("zelena");
    if (t == nullptr)
        cout << "nenasli" << endl;
    else
    {
        cout << "nasli " << t->name << ", " << t->r << ", " << t->g << ", " << t->b << endl;
        b.insertAfter (t, "svetle " + t->name, 128, t->g, 128);
    }
 
    cout << endl;
    b.print ();
 
    List c;
 
    while (b.last != NULL)
    {
        Item * t = b.last;
        b.unlink (t);
        c.linkLast (t);
    }
 
    cout << endl;
    c.print ();
 
    return 0;
}
 
zpro/list3st.txt · Last modified: 2020/11/19 15:15 by 88.103.111.44
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki