#include "stdafx.h"
 
#include <stdlib.h>
#include <iostream>
#include <cassert>
#include <string>
 
using namespace std;
 
class T
{
public:
    string data;
    int r, g, b;
    T* prev;
    T* next;
 
    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 () { }
 
    void place (T* prvek);
    void placeTo (T* prvek, T* ref);
    void Check ();
    void print ();
};
 
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();
 
    system ("pause");
    return 0;
}
 
seznam2018b.txt · Last modified: 2018/02/20 11:09 by 147.32.8.110
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki