#include "pch.h"
#include <iostream>
#include <cassert>
#include <string>
 
 
using namespace std;
 
class Item;
 
class List 
{
public:
	Item* first;
	Item* last;
 
	List() : first(nullptr), last(nullptr) {}
    ~ List ();
 
    void insertFirst(Item *p);
    void insertLast(Item *p);
    void add(string s);
	void print();
	Item* search(string s);
 
private:
    void link(Item* a, Item* b, Item* c);
    friend class Item;
};
 
class Item {
	public:
		string name;
		Item* prev;
		Item* next;
		List* owner;
		Item(string pocatek = ""): name(pocatek), prev(nullptr), next(nullptr),
		owner(NULL) {}
        void insertNext(Item* p);
        void insertPrev(Item* p);
        void unlink();
};
 
List::~List()
{
    Item* p = first;
    while (p != nullptr)
    {
        Item* t = p->next;
        delete p;
        p = t;
    }
 
    first = nullptr;
    last = nullptr;
}
 
void List::insertLast(Item *p) {
	assert(p != nullptr);
	p->owner = this;
	p->prev = this->last;
	p->next = nullptr;
	if (this->last != nullptr)
		this->last->next = p;
	else
		this->first = p;
	this->last = p;	
}
 
void List::add(string s) { insertLast(new Item(s)); }
 
void List::print()
{
	Item* p = first;
	while (p != nullptr)
	{
		cout << p->name << endl;
		p = p->next;
	}
}
 
Item* List::search(string s)
{
	Item* p = first;
	while (p != nullptr && p->name != s) p = p->next;
    // p == nullptr || p->name == s
	return p;
}
 
void List::link(Item* a, Item* b, Item* c) // b ..novy, a..predchudce b, c ... naslednik b
{
	assert(b != nullptr);
	assert(b->owner == nullptr);
 
	b->owner = this;
	b->next = c;
	b->prev = a;
 
	if (a != NULL)
		a->next = b;
	else
		first = b;
 
	if (c != NULL)
		c->prev = b;
	else
		last = b;
}
 
void Item::unlink() // x.unlink()  ... odpojit x
{
    assert(this != nullptr);
    assert(owner != nullptr);
 
    if (prev != NULL)
        prev->next = next;
    else
        owner->first = next;
 
    if (next != NULL)
        next->prev = prev;
    else
        owner->last = prev;
 
    owner = nullptr;
    next = nullptr;
    prev = nullptr;
}
 
void Item::insertNext(Item* p)
{
	assert(this != nullptr);
	assert(owner != nullptr);
	owner->link(this, p, next);
}
 
void Item::insertPrev(Item* p) // x.insertPrev(y) ... nove y vlozit pred x
{
    assert(this != nullptr);
    assert(owner != nullptr);
    owner->link(prev, p, this);
}
 
void List::insertFirst(Item *p) 
{
    link (nullptr, p, first);
}
 
List a;
// Item t("10");
// Item u;
 
int main()
{
	Item *test = new Item("ahoj"); 
	a.insertLast(test);
 
	a.insertLast(new Item("10"));
 
	a.add("abc");
 
	Item * t = a.search("10");
	// if (t != nullptr) cout << "nasli"; else cout << "nenasli";
    if (t != nullptr) t->insertNext (new Item ("11"));
 
    a.print();
 
    cout << "------" << endl;
 
    List b;
    while (a.first != nullptr)
    {
        Item * t = a.first;
        t->unlink ();
        b.insertFirst (t);
    }
 
    b.print();
 
    cout << "O.K." << endl;
}
 
seznam_2019_ut.txt · Last modified: 2019/03/05 11:10 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