#include <iostream>
using namespace std;
struct Item
{
string name;
string desc;
Item * next;
};
Item * first = nullptr; // nullptr ... prazdny seznam
void tisk ()
{
cout << "[" << endl;
Item * p = first;
if (p == nullptr)
cout << "Prazny seznam" << endl;
while (p != nullptr)
{
cout << p->name << ": " << p->desc << endl;
p = p->next;
}
cout << "]" << endl << endl;
}
int main()
{
tisk ();
first = new Item;
first->name = "Amalka";
first->desc = "vila";
first->next = nullptr;
tisk ();
Item * p = new Item;
p->name = "Mikes";
p->desc = "kocour";
first->next = p; // 1
p->next = u; // 2
u->next = nullptr; // 3
tisk ();
// cout << first->name << endl;
// cout << first->next->name << endl;
return 0;
}
#include <iostream>
using namespace std;
struct Item
{
string name;
string desc;
Item * next;
};
Item * first = nullptr; // nullptr ... prazdny seznam
void tisk ()
{
cout << "[" << endl;
Item * p = first;
if (p == nullptr)
cout << "Prazny seznam" << endl;
while (p != nullptr)
{
cout << p->name << ": " << p->desc << endl;
p = p->next;
}
cout << "]" << endl << endl;
}
void vloz_na_zacatek (string name0, string desc0)
{
Item * p = new Item;
p->name = name0;
p->desc = desc0;
p->next = first;
first = p;
}
void vloz_na_konec (string name0, string desc0)
{
Item * p = new Item;
p->name = name0;
p->desc = desc0;
p->next = nullptr;
if (first == nullptr)
{
first = p;
}
else
{
Item * t = first; // hledam posledniho
while (t->next != nullptr)
{
t = t->next;
}
t->next = p; ;
}
}
Item * hledej (string name0)
{
Item * p = first;
while (p != nullptr && p->name != name0)
{
p = p->next;
}
return p;
}
void vymaz (Item * p)
{
if (p != nullptr)
{
if (first == p)
first->next = p->next;
else
{
Item * t = first;
while (t != nullptr && t->next != p)
t = t->next;
if (t!= nullptr)
t->next = p->next;
}
delete p;
}
}
int main()
{
tisk ();
first = new Item;
first->name = "Amalka";
first->desc = "vila";
first->next = nullptr;
Item * p = new Item;
p->name = "Mikes";
p->desc = "kocour";
Item * u = new Item;
u->name = "Kremilek";
u->desc = "skritek";
first->next = p; // 1
p->next = u; // 2
u->next = nullptr; // 3
vloz_na_konec ("Rumcajz", "loupeznik");
vloz_na_konec ("Cipisek", "mensi loupeznik");
vloz_na_zacatek ("Vochomurka", "skritek");
vloz_na_zacatek ("Racochejl", "skritek");
vymaz (hledej ("Kremilek"));
tisk ();
// cout << first->name << endl;
// cout << first->next->name << endl;
// cout << first->next->next->name << endl;
return 0;
}